MayMatrix 2018-08-06
参考链接:https://www.cnblogs.com/kanyun/p/7159232.html
https://www.cnblogs.com/kanyun/p/7159232.html
因为项目是前后端分离,且跨域访问。采用form表单提交难以实现。故需要引入formdata,采用ajax提交。
参考地址中已经写的很详细了。这里只做个代码简化
前端代码:
<form enctype="multipart/form-data" id="formfile" class="upload-img"> <input type="file" multiple="multiple" /> </form>
$(document).on("change", ".upload-img input:file", function() { var t_files = this.files; var data = new FormData(); for (var i=0;i<t_files.length;i++){ data.append('file',t_files[i]); } $.ajax({ url : 'http://localhost:8080/YDplatform/apply/uploadScanFiles', //用于文件上传的服务器端请求地址 type : 'post', processData: false, // 告诉jQuery不要去处理发送的数据 contentType: false, // 告诉jQuery不要去设置Content-Type请求头 data:data, cache: false }).done(function(data,status){ var dataObj = jQuery.parseJSON(data); if(dataObj.state == -1){ new Message().showMsg("上传图片出错"); }else{ } }).fail(function (res) { }) });
后端代码:
@ResponseBody @RequestMapping(value = "uploadScanFiles", method = RequestMethod.POST) public HttpResponse uploadScanFiles(HttpServletRequest request, @RequestParam(value = "file", required = true) List<MultipartFile> files, String key, Long applyId, Long appId) throws Exception { if (null == files || 0 == files.size()) { throw BizException.create(Code.ParamError, "文件为空"); } for (MultipartFile file : files) { System.out.println("文件名:" + file.getOriginalFilename() + "---contentType:" + file.getContentType()); } return HttpResponse.OK(); }
注意:前端formdata的参数名和后台注解@RequestParam的value要对应上。后端接收参数里面除了文件以外,还有其它字段参数,也是可以用formdata来append的,这里就不改动了。
文件下载:
参考链接:https://blog.csdn.net/a447332241/article/details/78998239
前端代码:
ajax请求无法响应下载功能。因为response原因,一般请求浏览器是会处理服务器输出的response,例如生成png、文件下载等,然而ajax请求只是个“字符型”的请求,即请求的内容是以文本类型存放的。文件的下载是以二进制形式进行的,虽然可以读取到返回的response,但只是读取而已,是无法执行的,说白点就是js无法调用到浏览器的下载处理机制和程序。
推荐使用这种方式 自己构建表单进行提交
var form = $("<form>"); form.attr("style","display:none"); form.attr("target",""); form.attr("method","post"); form.attr("action",rootPath + "T_academic_essay/DownloadZipFile.do"); var input1 = $("<input>"); input1.attr("type","hidden"); input1.attr("name","strZipPath"); input1.attr("value",strZipPath); $("body").append(form); form.append(input1); form.submit(); form.remove();
后端代码有两种方式,我采用的后者:
方式一:直接贴代码
@RequestMapping("/download") public String download( String fileName ,String filePath, HttpServletRequest request, HttpServletResponse response){ response.setContentType("text/html;charset=utf-8"); try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } java.io.BufferedInputStream bis = null; java.io.BufferedOutputStream bos = null; String downLoadPath = filePath; //注意不同系统的分隔符 // String downLoadPath =filePath.replaceAll("/", "\\\\\\\\"); //replace replaceAll区别 ***** System.out.println(downLoadPath); try { long fileLength = new File(downLoadPath).length(); response.setContentType("application/x-msdownload;"); response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(downLoadPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (Exception e) { e.printStackTrace(); } finally { if (bis != null) try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (bos != null) try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
方式二:利用springmvc提供的ResponseEntity类型,使用它可以很方便地定义返回的HttpHeaders和HttpStatus。
RequestMapping("/download") public ResponseEntity<byte[]> export(String fileName,String filePath) throws IOException { HttpHeaders headers = new HttpHeaders(); File file = new File(filePath); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", fileName); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }
如果是多文件下载,则需要进行压缩处理:
参考链接:https://www.cnblogs.com/zeng1994/p/7862288.html文
Controller:
@RequestMapping(value = "downloadPackage", method = RequestMethod.GET) public void downloadPackage2(HttpServletRequest request, @RequestParam(required = true) String key, Integer processType, Long applyId, HttpServletResponse response) throws Exception { String fileDir = applicationService.getPackagefileDirByProcessAndKey(applyId, key, processType); if (StringUtils.isBlank(fileDir)) { throw BizException.create(Code.NOTEXIST, "文件没找到!"); } String rootPath = request.getSession().getServletContext().getRealPath("/"); String tempFileName = rootPath + "/" + UUID.randomUUID().toString().replaceAll("-", ""); File temDir = new File(tempFileName); if (!temDir.exists()) { temDir.mkdirs(); } File resource = new File(fileDir); FileUtils.copyDirectory(resource, temDir); response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=package_" + System.currentTimeMillis() + ".zip"); ZipUtils.toZip(temDir.getPath(), response.getOutputStream(), false); File[] listFiles = temDir.listFiles(); for (int i = 0; i < listFiles.length; i++) { listFiles[i].delete(); } temDir.delete(); }
Utils:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * * ZipUtils * * @author ZENG.XIAO.YAN * * @date 2017年11月19日 下午7:16:08 * * @version v1.0 * */ public class ZipUtils { private static final int BUFFER_SIZE = 2 * 1024; /** * * 压缩成ZIP 方法1 * * @param srcDir * 压缩文件夹路径 * * @param out * 压缩文件输出流 * * @param KeepDirStructure * 是否保留原来的目录结构,true:保留目录结构; * * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * * @throws RuntimeException * 压缩失败会抛出运行时异常 * */ public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException { long start = System.currentTimeMillis(); ZipOutputStream zos = null; try { zos = new ZipOutputStream(out); File sourceFile = new File(srcDir); compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure); long end = System.currentTimeMillis(); System.out.println("压缩完成,耗时:" + (end - start) + " ms"); } catch (Exception e) { throw new RuntimeException("zip error from ZipUtils", e); } finally { if (zos != null) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * 压缩成ZIP 方法2 * * @param srcFiles * 需要压缩的文件列表 * * @param out * 压缩文件输出流 * * @throws RuntimeException * 压缩失败会抛出运行时异常 * */ public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException { long start = System.currentTimeMillis(); ZipOutputStream zos = null; try { zos = new ZipOutputStream(out); for (File srcFile : srcFiles) { byte[] buf = new byte[BUFFER_SIZE]; zos.putNextEntry(new ZipEntry(srcFile.getName())); int len; FileInputStream in = new FileInputStream(srcFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } zos.closeEntry(); in.close(); } long end = System.currentTimeMillis(); System.out.println("压缩完成,耗时:" + (end - start) + " ms"); } catch (Exception e) { throw new RuntimeException("zip error from ZipUtils", e); } finally { if (zos != null) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * 递归压缩方法 * * @param sourceFile * 源文件 * * @param zos * zip输出流 * * @param name * 压缩后的名称 * * @param KeepDirStructure * 是否保留原来的目录结构,true:保留目录结构; * * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * * @throws Exception * */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { byte[] buf = new byte[BUFFER_SIZE]; if (sourceFile.isFile()) { // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip输出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } // Complete the entry zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) { // 需要保留原来的文件结构时,需要对空文件夹进行处理 if (KeepDirStructure) { // 空文件夹的处理 zos.putNextEntry(new ZipEntry(name + "/")); // 没有文件,不需要文件的copy zos.closeEntry(); } } else { for (File file : listFiles) { // 判断是否需要保留原来的文件结构 if (KeepDirStructure) { // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } } public static void main(String[] args) throws Exception { /** 测试压缩方法1 */ FileOutputStream fos1 = new FileOutputStream(new File("c:/mytest01.zip")); ZipUtils.toZip("D:/log", fos1, true); /** 测试压缩方法2 */ List<File> fileList = new ArrayList<>(); fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/jar.exe")); fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/java.exe")); FileOutputStream fos2 = new FileOutputStream(new File("c:/mytest02.zip")); ZipUtils.toZip(fileList, fos2); } }
在新项目中使用的是springboot编写的api,涉及到ajax跨域请求和传输文件的问题,在这里记录一下。<input type="button" onclick="test();" value="