在http中能下载,但是启用https协议后下载报错问题结果,在谷歌,火狐浏览器正常
具体操作如下:
// 获取下载文件 //userRegisterInfoPath 下载路径 File downloadFile = new File(userRegisterInfoPath); FileInputStream fos = new FileInputStream(downloadFile); byte[] bytes = new byte[4096]; int read = 0; response.reset(); response.setContentType("application/octet-stream;charset=UTF-8"); //filename 是带扩展名 response.setHeader("Content-Disposition","attachment; filename="+ URLEncoder.encode("申请表.pdf", "utf-8")); response.setHeader("Pragma", "public"); //下载设置的关键项 response.setHeader("Cache-Control","public"); ServletOutputStream sos = response.getOutputStream(); while((read=fos.read(bytes))!=-1){ sos.write(bytes, 0, read); } sos.flush(); sos.close();
文件上传转载的一片好文:
作者:http://www.blogjava.net/leekiang/archive/2007/08/27/139844.html
片段代码
<form action="" method="post" enctype="multipart/form-data">
最大上传2G.
通过 http 协议上传文件(rfc1867协议概述,jsp 应用举例,客户端发送内容构造)
服务器接收到上传的流,自己其实是不作任何处理的,那个request还是原装的,谁来处理这个request呢,一般采用第三方的工具,这里以commons fileupload为例.
DiskFileItemFactory factory =
new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲,这个值决定了是fileinputstream还是bytearrayinputstream
factory.setRepository(new File("d:\\temp")); // 设置临时存放目录,默认是new File(System.getProperty("java.io.tmpdir"))
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setSizeMax(100*1024*1024); // 100M
List items = sfu.parseRequest(request); // 传入的这个request还是原装的
factory.setSizeThreshold(4096); // 设置缓冲,这个值决定了是fileinputstream还是bytearrayinputstream
factory.setRepository(new File("d:\\temp")); // 设置临时存放目录,默认是new File(System.getProperty("java.io.tmpdir"))
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setSizeMax(100*1024*1024); // 100M
List items = sfu.parseRequest(request); // 传入的这个request还是原装的
见上面的代码,commons fielupload通过ServletFileUpload类的parseRequest(request)方法处理这个原始流。而ServletFileUpload又会调用其爷爷类FileUploadBase的parseRequest(request)方法,然后又会调return parseRequest(new ServletRequestContext(request)),代码如下
上传代码springboot
@PostMapping(value="/szjrb/upLoadReportXmlFile") public RestModel upLoadReportXmlFile(MultipartHttpServletRequest multiReq,String xmlFilePath) throws IOException{ FileOutputStream fos = null; FileInputStream fis = null; try{ if(StringUtils.isBlank(xmlFilePath)){ return new RestModel("10006","xmlFilePath参数不能为空"); } File file = new File(xmlFilePath); if(!file.isFile()){ return new RestModel("10007","xmlFilePath不是文件路径"); } //上传 fos=new FileOutputStream(new File(xmlFilePath)); fis=(FileInputStream) multiReq.getFile("file").getInputStream(); byte[] buffer=new byte[1024]; int len=0; while((len=fis.read(buffer))!=-1){ fos.write(buffer, 0, len); } }catch (Exception e){ logger.error(e.getMessage(),e); return new RestModel("0","上传失败",xmlFilePath); }finally { fos.close(); fis.close(); } return new RestModel(RestModel.CODE_SUCCESS,RestModel.MESSAGE_SUCCESS,xmlFilePath); }