场景一 download from webserver ,source file come from webserver file system
/** * 文件下载 */ @Controller @RequestMapping(value = "file/",method=RequestMethod.GET) public class FileSvcController extends BaseController { @RequestMapping("download") public HttpServletResponse download(HttpServletRequest request, HttpServletResponse response) throws IOException { try { //web服务器上文件存储路径 String filePath = "d:\\星空.jpg"; java.io.File file = new java.io.File(filePath); //在浏览器上显示的下载文件名 String fileName = "星空.jpg"; // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(filePath)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header URLEncoder对文件名编码输出,防止中文文件名出现乱码 response.addHeader("Content-Disposition", "attachment;filename=" +java.net.URLEncoder.encode(fileName, "UTF-8")); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; } }
场景二 download from webserver ,source file come from remote server file system
/** * 文件下载 */ @Controller @RequestMapping(value = "file/",method=RequestMethod.GET) public class FileSvcController extends BaseController { final static int BUFFER_SIZE = 4096; @RequestMapping("download") public HttpServletResponse download(HttpServletRequest request, HttpServletResponse response){ InputStream fis = null; try { String fileName = "星空.jpg"; String fileUrl = "http://192.168.1.105/2016/11/26/"+URLEncoder.encode("星空.jpg","UTF-8"); URL uri = new URL(fileUrl); InputStream fileStream = uri.openStream(); // 以流的形式下载文件。 fis = new BufferedInputStream(fileStream); byte[] buffer = InputStreamTOByte(fis); fis.close(); // 清空response response.reset(); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8")); response.addHeader("Content-Length", "" + buffer.length); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; } /** * 循环从inputstream中读取文件流到byte数组 * 将inputstream convert to byte array * @param in * @return * @throws IOException */ public byte[] InputStreamTOByte(InputStream in) throws IOException{ ByteArrayOutputStream outStream = null; try { outStream = new ByteArrayOutputStream(); byte[] data = new byte[BUFFER_SIZE]; int count = -1; while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) outStream.write(data, 0, count); data = null; outStream.close(); return outStream.toByteArray(); }catch (IOException e){ throw e; } finally { if(outStream != null) outStream.close(); } } }
其他
测试的时候是使用自己的家用笔记本电脑(作为文件服务器)和公司配置的笔记本电脑(作为web服务器),自己的家用笔记本电脑作为文件服务器(windows操作系统),自己的笔记本电脑可以使用Http://192.168.1.105/2016/11/26/00c62dc866ce49f8a421602c21ce61f0.pdf访问到文件,但是使用公司的电脑(ip为192.168.1.115)无法访问到Http://192.168.1.105/2016/11/26/00c62dc866ce49f8a421602c21ce61f0.pdf文件,原来是文件服务器开启了防火墙导致的,将文件服务器防火墙关闭即可正常访问文件服务器下载查看文件