116 lines
4.3 KiB
Plaintext
116 lines
4.3 KiB
Plaintext
|
|
package com.bonus.utils;
|
||
|
|
import java.io.BufferedOutputStream;
|
||
|
|
import java.io.ByteArrayOutputStream;
|
||
|
|
import java.io.File;
|
||
|
|
import java.io.FileInputStream;
|
||
|
|
import java.io.FileNotFoundException;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.io.InputStream;
|
||
|
|
import java.io.OutputStream;
|
||
|
|
import java.io.UnsupportedEncodingException;
|
||
|
|
import java.net.URLEncoder;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.Iterator;
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
import javax.servlet.ServletOutputStream;
|
||
|
|
import javax.servlet.http.HttpServletRequest;
|
||
|
|
import javax.servlet.http.HttpServletResponse;
|
||
|
|
|
||
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
||
|
|
import org.springframework.web.multipart.MultipartFile;
|
||
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||
|
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||
|
|
public class UploadUtils {
|
||
|
|
public static HashMap<String, Object> uploadFileSpring(HttpServletRequest request) {
|
||
|
|
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
|
||
|
|
//判断 request 是否有文件上传,即多部分请求
|
||
|
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
||
|
|
List<MultipartFile> tmps = new ArrayList<MultipartFile>();
|
||
|
|
if(multipartResolver.isMultipart(request)){
|
||
|
|
//转换成多部分request
|
||
|
|
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
|
||
|
|
Iterator iter = multiRequest.getFileNames();
|
||
|
|
while(iter.hasNext()){
|
||
|
|
MultipartFile file = multiRequest.getFile((String) iter.next());
|
||
|
|
tmps.add(file);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
map.put("filePath", tmps);
|
||
|
|
return map;
|
||
|
|
}
|
||
|
|
//模板下载工具类
|
||
|
|
static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
|
||
|
|
try {
|
||
|
|
response.setCharacterEncoding("UTF-8");
|
||
|
|
response.setHeader("content-Type", "application/vnd.ms-excel");
|
||
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||
|
|
workbook.write(response.getOutputStream());
|
||
|
|
} catch (IOException e) {
|
||
|
|
// throw new NormalException(e.getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 将文件转换成byte数组
|
||
|
|
* @param filePath
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static byte[] File2byte(InputStream fis){
|
||
|
|
byte[] buffer = null;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||
|
|
byte[] b = new byte[1024];
|
||
|
|
int n;
|
||
|
|
while ((n = fis.read(b)) != -1)
|
||
|
|
{
|
||
|
|
bos.write(b, 0, n);
|
||
|
|
}
|
||
|
|
fis.close();
|
||
|
|
bos.close();
|
||
|
|
buffer = bos.toByteArray();
|
||
|
|
}catch (FileNotFoundException e){
|
||
|
|
e.printStackTrace();
|
||
|
|
}catch (IOException e){
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
return buffer;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void writeFile2Response(HttpServletResponse response, InputStream inputStream, String fileName) throws UnsupportedEncodingException {
|
||
|
|
response.setContentType("application/x-download;charset=UTF-8");
|
||
|
|
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));
|
||
|
|
OutputStream os = null;
|
||
|
|
ServletOutputStream ros = null;
|
||
|
|
try {
|
||
|
|
ros = response.getOutputStream();
|
||
|
|
os = new BufferedOutputStream(ros);
|
||
|
|
byte[] bs = UploadUtils.File2byte(inputStream);
|
||
|
|
os.write(bs);
|
||
|
|
os.flush();
|
||
|
|
ros.flush();
|
||
|
|
} catch (IOException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
} finally {
|
||
|
|
if (os != null) {
|
||
|
|
try {
|
||
|
|
os.close();
|
||
|
|
} catch (IOException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (ros != null) {
|
||
|
|
try {
|
||
|
|
ros.flush();
|
||
|
|
ros.close();
|
||
|
|
} catch (IOException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|