不啰嗦,直接上代码
import ;
import ;
import .slf4j.Slf4j;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* @NAME: hzan
* @DATE: 2022/11/17
* 文件上传
**/
@Slf4j
public class import {
/**
* 文件上传
* ModelAndView 响应 可以自己定义响应类型
* @param req
* @param res
* @return
* @throws Exception
*/
public ModelAndView importFile(HttpServletRequest req, HttpServletResponse res) throws Exception {
try {
("图片上传开始");
//上传存放文件的路径
String imageUploadPath="url";
("图片上传地址:"+imageUploadPath);
JSONObject jj = new JSONObject();
MultipartHttpServletRequest multiPartReq = (MultipartHttpServletRequest) req;
MultipartFile tfile = null;
//前段传的特殊标识自行判断
String type = ("type");
if((type)) {
tfile = (("type"));
}else {
tfile = ("file");
}
//获取文件名
String filename = ();
String reg = ".+(docx|DOCX|DOC|doc|PDF|pdf|JPEG|jpeg|JPG|jpg|GIF|gif|BMP|bmp|PNG|png)$";
Pattern pattern = (reg);
Matcher matcher = (filename);
//设置响应状态
rendText(res);
//1024*1024=1MB 比较文件大小,和文件格式
if(() > 1024*1024 && ()) {
("result", "err");
("message", "文件过大");
}else {
FilePathVO file = uploadFile(tfile, imageUploadPath);
if(file != null && (()) ) {
("data", file);
("result", "success");
("message", "上传成功");
}else {
("result", "error");
("message", "上传失败");
}
}
().print(());
().close();
("文件上传结束");
return null;
}catch (Exception e) {
("图片上传异常:"+());
();
}
return null;
}
private void rendText(HttpServletResponse res) {
("text/plain");
("UTF-8");
("Pragma", "no-cache");
("Cache-Control", "no-cache, must-revalidate");
("Pragma", "no-cache");
}
/**
* 上传单个图片/文件
*
* @param upload
* @param path 服务地址
* @return
* @throws IOException
*/
public static FilePathVO uploadFile(MultipartFile upload, String path) throws IOException {
FilePathVO filePathDTO = null;
String content = null;
CloseableHttpClient httpClient = ();
HttpPost httpPost = new HttpPost(path);
MultipartEntityBuilder builder = ();
RequestConfig requestConfig = ()
.setConnectTimeout(15000).setConnectionRequestTimeout(11000)
.setSocketTimeout(15000).build();
(requestConfig);
if (upload != null && () != null && .(())) {
String filename = ();
("file", (), ContentType.MULTIPART_FORM_DATA, filename);
try {
HttpEntity entity = ();
(entity);
CloseableHttpResponse response = (httpPost);
InputStream in = ().getContent();
content = (in, "utf-8");
// (content);
// (().getStatusCode());
if (200==().getStatusCode()) {
filePathDTO = (content, );
} else {
return null;
}
} catch (Exception e) {
();
} finally {
();
}
}
return filePathDTO;
}
/**
* 上传多个文件
*
* @param uploads
* @param path 服务地址
* @return
* @throws IOException
*/
public static FileListVO uploadFiles(MultipartFile uploads[], String path) throws IOException {
FileListVO fileListVO = new FileListVO();
String content = null;
CloseableHttpClient httpClient = ();
HttpPost httpPost = new HttpPost(path);
MultipartEntityBuilder builder = ();
if (uploads != null && > 0) {
for (int i = 0; i < ; i++) {
if (uploads[i] != null && uploads[i].getInputStream() != null && .(uploads[i].getOriginalFilename())) {
String filename = uploads[i].getOriginalFilename();
("files", uploads[i].getBytes(), ContentType.MULTIPART_FORM_DATA, filename);
}
}
}
try {
HttpEntity entity = ();
(entity);
CloseableHttpResponse response = (httpPost);
InputStream in = ().getContent();
content = (in, "utf-8");
if ("200".equals(())) {
fileListVO = (FileListVO) (content, );
} else {
return null;
}
} catch (Exception e) {
();
} finally {
();
}
return fileListVO;
}
}