public String downLoadFileContent() throws IOException{ // 下载网络文件 int bytesum = 0; int byteread = 0; response = CustomerUtil.getResponse(); request = CustomerUtil.getRequest(); String filePath = request.getParameter("filePath");//http://localhost:8080/mypro/img String fileName = request.getParameter("fileName");//201212121212.txt URL url = new URL(filePath+fileName); try { URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); OutputStream fs=response.getOutputStream(); //设置response的编码方式 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition","p_w_upload;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1")); response.setHeader("Content-Disposition","p_w_upload;filename="+fileName); byte[] buffer = new byte[1204]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; fs.write(buffer, 0, byteread); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
java 下载文件功能
原创
©著作权归作者所有:来自51CTO博客作者小浩51的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
centos7用命令上传、下载文件
centos7用命令上传、下载文件
centos linux 上传下载 -
sevlet实现下载文件功能
希望做一个小板块,实现文件的上传和下载,那么上传实现了,就需要实现下载,阅读了各位的博客总结了一下。在网页中通过超
servlet java 文件名 绝对路径