java 远程下载(服务器)文件(弹框下载)

1.目的

   下载远程文件

效果如图

IE下

chrome下



2.流程

1.提交下载请求(有坑,不能用ajax,或ajaxsubmit直接提交)

2.读取文件

3.response回显

 

3.代码

 

1.前台jsp/html

这个form是用来提交下载请求的,  其中的input可以传递一些参数,  action就是后台的request请求地址

<%--  用于下载提交请求--%>
  <form id="downloadForm" action="${BASEPATH}annexManage/download" method="post" type="hidden">
    <input id="filePath" name="filePath" value="" type="hidden"/>
    <input id="fileName" name="fileName" value="" type="hidden"/>
    <input id="extName" name="extName" value="" type="hidden"/>
  </form>

</div>

2.js代码

给input的属性赋值

使用submit提交请求

一定要这样提交,不能用$.ajax或者 ajaxsubmit。 不然不会下载

$("#filePath").val(filePath);
$("#fileName").val(rowData.aFilename);
 $("#extName").val(rowData.aFiletype);
$("#downloadForm").submit();

 

3.java后台代码

    @RequestMapping(value = "/getFileStream/", method = RequestMethod.GET)
    public void pdfStreamHandler(HttpServletRequest request, HttpServletResponse response) {
    	String fileSrc=(String) request.getSession().getAttribute("fileSrc");
    	logger.debug("获取的预览文件路径是 "+ fileSrc);
        File file = new File(fileSrc);
        try {
            byte[] data = null;
            try {
            	String strUrl = fileSrc.trim();  
                URL url=new URL(strUrl);
                //打开请求连接
                URLConnection connection = url.openConnection();
                HttpURLConnection httpURLConnection=(HttpURLConnection) connection;
                httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                // 取得输入流,并使用Reader读取
                int responseCode=((HttpURLConnection) connection).getResponseCode();
                BufferedInputStream bis=null;
                	 bis = new BufferedInputStream(httpURLConnection.getInputStream());




                ServletOutputStream sos = response.getOutputStream();
				//BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
				int b;
				while((b = bis.read())!=-1) {
					sos.write(b);
				}
				sos.close();
				bis.close();
            } catch (Exception e) {
                System.out.println("pdf文件处理异常:" + e);
            }
        }
        catch (Exception e) {
        	 return;
		}
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
以下是Java代码示例,用于从远程服务器下载NFS文件: ```java import java.io.*; import java.net.InetAddress; import java.net.UnknownHostException; import com.sun.nio.file.ExtendedOpenOption; import com.sun.nio.file.SensitivityWatchEventModifier; import com.sun.nio.file.SynchronousFileChannel; import com.sun.nio.file.WatchEvent.Kind; import com.sun.nio.file.WatchKey; import com.sun.nio.file.WatchService; import sun.nio.fs.UnixChannelFactory; public class DownloadNFSFile { public static void main(String[] args) { String remoteFilePath = "/nfs/file.txt"; // 远程NFS文件路径 String localFilePath = "/local/file.txt"; // 本地保存路径 try { // 获取远程服务器地址 InetAddress remoteAddress = InetAddress.getByName("remote.server.com"); // 创建文件输入流 InputStream in = new NFSInputStream(remoteAddress, remoteFilePath); // 创建本地文件输出流 OutputStream out = new FileOutputStream(localFilePath); // 缓冲区大小 byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } // 关闭流 in.close(); out.close(); System.out.println("文件下载成功!"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 自定义NFSInputStream类,用于从远程NFS文件读取数据 */ class NFSInputStream extends InputStream { private SynchronousFileChannel channel; public NFSInputStream(InetAddress remoteAddress, String remoteFilePath) throws IOException { UnixChannelFactory factory = new UnixChannelFactory(); channel = factory.newAsynchronousFileChannel(remoteAddress, remoteFilePath, null, null, null); } @Override public int read() throws IOException { return 0; } @Override public int read(byte[] b, int off, int len) throws IOException { return channel.read(ByteBuffer.wrap(b, off, len), 0).get(); } @Override public void close() throws IOException { channel.close(); } } ``` 需要注意的是,上述代码使用了Java 7中的NIO.2 API,因此需要在Java 7或更高版本上运行。同时,需要添加NFS客户端软件包的依赖。在Linux系统上,可以安装`nfs-common`软件包来提供NFS客户端支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值