JavaWeb实现多文件上传及zip打包下载

时间:2018-07-14
这篇文章主要为大家详细介绍了JavaWeb实现多文件上传及zip打包下载,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了javaweb多文件上传及zip打包下载的具体代码,供大家参考,具体内容如下

项目中经常会使用到文件上传及下载的功能。本篇文章总结场景在JavaWeb环境下,多文件上传及批量打包下载功能,包括前台及后台部分。

首先明确一点:

无法通过页面的无刷新ajax请求,直接发下载、上传请求。上传和下载,均需要在整页请求的基础上实现。项目中一般通过构建form表单形式实现这一功能。

一、多文件上传

项目需求为实现多图片上传功能。参考测试了网上找到的众多插件方法后,决定选用Jquery原始上传方案。以下按步骤贴出具体代码。

1、HTML部分(可省略使用js构建)

<form id="uploadForm" method="post" enctype="multipart/form-data">
 <input type="file" hidden name="fileImage" multiple/>
 <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" id="fileSubmit" onclick="uploadFileMulti()">上传资料</a>
</form>

有几点说明:

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<MultipartFile> files = mRequest.getFiles("fileImage");

以上有几点说明:

1. 获取MultipartHttpServletRequest,对应file标签的name

二、文件批量下载

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

HTML部分(可省略使用js构建)

<form id="uploadForm" method="post" enctype="multipart/form-data">
 <div class="product-dl">
  <input type="hidden" name="orderCode"/>
  <input type="hidden" name="datumType"/>
  <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" class="btn" onclick="batchDatumListDownLoad()">批量下载</a>
 </div>
</form>

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<DatumListVo> voList = datumListService.queryDatumLists(datumListVo);

  //压缩文件
  List<File> 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

/**
 * 压缩文件列表中的文件
 *
 * @param files
 * @param outputStream
 * @throws IOException
 */
public static void zipFile(List files, ZipOutputStream outputStream) throws IOException, 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文件中
 *
 * @param inputFile
 * @param outputstream
 * @throws Exception
 */
public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, 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; //最大的流为10M
    long 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 entry
    bInStream.close(); //关闭
    inStream.close();
   }
  } else {
   throw new ServletException("文件不存在!");
  }
 } catch (IOException e) {
  throw e;
 }
}

/**
 * 下载打包的文件
 *
 * @param file
 * @param response
 */
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();
  // 清空response
  response.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();
 }
}

以上基本满足文件上传下载所需。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。