项目中经常会使用到文件上传及下载的功能。本篇文章总结场景在JavaWeb环境下,多文件上传及批量打包下载功能,包括前台及后台部分。首先明确一点:无法通过页面的无刷新ajax请求,直接发下载、上传请求。上传和下载,均需要在整页请求的基础上实现。项目中一般通过构建form表单形式实现这一功能。

一、多文件上传

项目需求为实现多图片上传功能。参考测试了网上找到的众多插件方法后,决定选用Jquery原始上传方案。以下按步骤贴出具体代码。 1、HTML部分(可省略使用js构建)


上传资料

有几点说明: 1. form中enctype=”multipart/form-data”2.例中使用标签,构建submit

2、JS部分 var formData = new FormData($("#uploadForm")[0]);formData.append("foldName", "datumList");//设置父级文件夹名称formData.append("oderCode", selfOrderCode);formData.append("datumType", datumType);$.ajax({type: "POST",data: formData,url: "order/datumList/batchInsertDatumLists",contentType:false,processData: false,success:function(result){if (result.success) {//清空框文件内容$("#fileImage").val("");var obj =document.getElementById('fileImage');obj.outerHTML = obj.outerHTML;refreshDatumList();showSuccessToast(result.message);}else {showWarningToast(result.message);}},error: function(){showErrorToast('请求失败!')}});以上有几点说明:1. var formData = new FormData($(“#uploadForm”)[0]);

2.使用formData.append(“oderCode”, selfOrderCode);添加其他参数Java后台MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; List files = mRequest.getFiles("fileImage");以上有几点说明:1.获取MultipartHttpServletRequest,对应file标签的name

二、文件批量下载

本项目中,需求为批量下载某一批次文件。使用zip在服务器压缩文件,之后将文件下载到客户机。网上查询,使用Java自带的文件输出类不能解决压缩文件中文件名乱码的问题。解决方法:使用ant.jar包,创建压缩文件时,可以设置文件的编码格式,文件名乱码的问题就解决了。 HTML部分(可省略使用js构建)


批量下载

JS部分//批量下载function batchDatumListDownLoad(){var param = {};param.datumType = $("#datumTypeQ").val();if (param.datumType ==-1) {param.datumType =null;//查询所有}param.orderCode = selfOrderCode;$("#uploadForm input[name=orderCode]").val(param.orderCode);$("#uploadForm input[name=datumType]").val(param.datumType);var form = $("#uploadForm")[0];form.action ="order/datumList/batchDownLoadDatumList";form.method = "post";form.submit();//表单提交}后台部分public void batchDownLoadDatumList(DatumListVo datumListVo, HttpServletResponse response) {try {//查询文件列表List voList = datumListService.queryDatumLists(datumListVo);//压缩文件List files =new ArrayList<>();for (DatumListVo vo : voList) {File file = new File(vo.getDatumUrl());files.add(file);}String fileName = datumListVo.getOrderCode() +"_" + datumListVo.getDatumType() + ".zip";//在服务器端创建打包下载的临时文件String globalUploadPath ="";String osName = System.getProperty("os.name");if (osName.toLowerCase().indexOf("windows") >=0) {globalUploadPath = GlobalKeys.getString(GlobalKeys.WINDOWS_UPLOAD_PATH);}else if (osName.toLowerCase().indexOf("linux") >=0 || osName.toLowerCase().indexOf("mac") >=0) {globalUploadPath = GlobalKeys.getString(GlobalKeys.LINUX_UPLOAD_PATH);}String outFilePath = globalUploadPath + File.separator + fileName;File file =new File(outFilePath);//文件输出流FileOutputStream outStream =new FileOutputStream(file);//压缩流ZipOutputStream toClient =new ZipOutputStream(outStream);//设置压缩文件内的字符编码,不然会变成乱码toClient.setEncoding("GBK");ZipUtil.zipFile(files, toClient);toClient.close();outStream.close();ZipUtil.downloadZip(file, response);}catch (Exception e) {e.printStackTrace();}}其中ZipUtil.java/*** 压缩文件列表中的文件**@paramfiles*@paramoutputStream*@throwsIOException*/public static void zipFile(List files, ZipOutputStream outputStream) throwsIOException, ServletException{try {int size = files.size();//压缩列表中的文件for (int i =0; i < size; i++) {File file = (File) files.get(i);try {zipFile(file, outputStream);} catch (Exception e) {continue;}}} catch (Exception e) {throw e;}}/*** 将文件写入到zip文件中**@paraminputFile*@paramoutputstream*@throwsException*/public static void zipFile(File inputFile, ZipOutputStream outputstream) throwsIOException, ServletException{try {if (inputFile.exists()) {if (inputFile.isFile()) {FileInputStream inStream = new FileInputStream(inputFile);BufferedInputStream bInStream = new BufferedInputStream(inStream);ZipEntry entry = new ZipEntry(inputFile.getName());outputstream.putNextEntry(entry);final int MAX_BYTE =10 * 1024 * 1024;//最大的流为10Mlong streamTotal =0;//接受流的容量int streamNum =0;//流需要分开的数量int leaveByte =0;//文件剩下的字符数byte[] inOutbyte;//byte数组接受文件的数据streamTotal = bInStream.available();                        //通过available方法取得流的最大字符数streamNum = (int) Math.floor(streamTotal / MAX_BYTE);//取得流文件需要分开的数量leaveByte = (int) streamTotal % MAX_BYTE;//分开文件之后,剩余的数量if (streamNum >0) {for (int j =0; j < streamNum; ++j) {inOutbyte =new byte[MAX_BYTE];//读入流,保存在byte数组bInStream.read(inOutbyte,0, MAX_BYTE);outputstream.write(inOutbyte, 0, MAX_BYTE);//写出流}}//写出剩下的流数据inOutbyte =new byte[leaveByte];bInStream.read(inOutbyte,0, leaveByte);outputstream.write(inOutbyte);outputstream.closeEntry();//Closes the current ZIP entry and positions the stream for writing the next entrybInStream.close();    //关闭inStream.close();}}else {throw new ServletException("文件不存在!");}}catch (IOException e) {throw e;}}/*** 下载打包的文件**@paramfile*@paramresponse*/public static void downloadZip(File file, HttpServletResponse response) {try {// 以流的形式下载文件。BufferedInputStream fis =new BufferedInputStream(new FileInputStream(file.getPath()));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();OutputStream toClient =new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());toClient.write(buffer);toClient.flush();toClient.close();file.delete();//将生成的服务器端文件删除}catch (IOException ex) {ex.printStackTrace();}}以上基本满足文件上传下载所需