1.html内容
<div>
<form method="post" action="/Cyberspace/main/informationBatchAdd.do" enctype="multipart/form-data">
<input type="file" id="excelFile" multiple="multiple" name="file"/>
<button type="submit">批量上传</button>
</form>
</div>
2.服务端后台支持多文件上传的接口
/**
* 信息上报批量增加
*/
@RequestMapping(value = "informationBatchAdd", method = RequestMethod.POST)
@ResponseBody
public JSONResult informationBatchAdd(MultipartHttpServletRequest request) {
JSONResult jsonResult;
try {
Integer result = 0;
MultiValueMap<String, MultipartFile> multiValues = request.getMultiFileMap();//获取请求中所有的文件流
Iterator<Map.Entry<String, List<MultipartFile>>> iterator = multiValues.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, List<MultipartFile>> entry = iterator.next();
for (MultipartFile file : entry.getValue()) {
InputStream in = file.getInputStream();//转换成输入流
XSSFWorkbook readWb = new XSSFWorkbook(in);
for (int i = 0; i < readWb.getNumberOfSheets(); i++) {//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
XSSFSheet sheet = readWb.getSheetAt(i);
。。。
Integer totalRow = sheet.getLastRowNum();//获取excel共有多少行
for (int rowNum = 1; rowNum <= totalRow; rowNum++) { // 循环行Row
XSSFRow hssfRow = sheet.getRow(rowNum);
if (hssfRow != null && hssfRow.getPhysicalNumberOfCells() >= 7) {//getPhysicalNumberOfCells 获取纵列数
。。。
}
}
result += 。。。;
}
}
}
jsonResult = new JSONResult(CODE_SUCCESS, MSG_SUCCESS, result);//JSONResult是自己定义的一个类
} catch (Exception e) {
jsonResult = new JSONResult(CODE_FAIL, MSG_FAIL, null);
e.printStackTrace();
}
return jsonResult;
}
3.假如遇到要中转上传文件流到以上的接口中,那么下面的内容就是你的福音
/**
* 中转文件
*
* @param files 上传的文件,该参数的获取 request.getFiles("xxxx")或者参照第二步中的方法
* @return 响应结果
*/
public static String httpClientUploadFile(List<MultipartFile> files, String remoteUrl) {
final String remote_url = remoteUrl;// 第三方服务器请求地址
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
HttpPost httpPost = new HttpPost(remote_url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//设置浏览器兼容模式
for (int i = 0; i < files.size(); i++) {
MultipartFile file = files.get(i);
String fileName = file.getOriginalFilename();
builder.addBinaryBody("file" + i, file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
}
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
result = EntityUtils.toString(responseEntity, UTF8);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
4.中转下载文件
@RequestMapping(value = "fileDownload", method = RequestMethod.GET)
@ResponseBody
public String fileDownload(HttpServletRequest request, HttpServletResponse response) {
String url;
try {
String fileName = StringUtils.isNotEmpty(request.getParameter("filename")) ? request.getParameter("filename") : new Date().getTime() + "";
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");//解决中文get方式乱码的问题url = "http://192.168.1.100:8188/examples/JSSDK.html";//我想下载这个JSSDK这个html文件
FileUtil.downloadFileFromNet(url, fileName, response);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** FiletUtil.downloadFileFromNet
* 从网络中下载文件
*/
public static void downloadFileFromNet(String urlPath, String fileName, HttpServletResponse response) throws MalformedURLException {
InputStream fis = null;
OutputStream os = null;
try {
URL url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
// 以流的形式下载文件
fis = new BufferedInputStream(urlConnection.getInputStream());
// 设置响应报头
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, ENCODING));
response.setCharacterEncoding(ENCODING);
// 写入响应流数据
os = new BufferedOutputStream(response.getOutputStream());
byte[] bytes = new byte[1024];
while (fis.read(bytes) != -1) {
os.write(bytes);
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}