package
cn.juhe.controller;
import
net.sf.json.JSONObject;
import
org.springframework.web.bind.annotation.PostMapping;
import
org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.multipart.MultipartFile;
import
org.springframework.web.multipart.MultipartHttpServletRequest;
import
javax.servlet.http.HttpServletRequest;
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.util.Date;
import
java.util.Iterator;
import
java.util.List;
import
java.util.Random;
@RestController
public
class
UploadTest {
/**
* 接受未知参数名的多个文件或者一个文件
*
* @param request 请求
* @return 返回
*/
@PostMapping
(
"/upload"
)
public
JSONObject handleFileUpload(HttpServletRequest request) {
Iterator<String> fileNames = ((MultipartHttpServletRequest) request).getFileNames();
JSONObject result =
null
;
while
(fileNames.hasNext()) {
String next = fileNames.next();
MultipartFile file = ((MultipartHttpServletRequest) request).getFile(next);
System.out.println(
"file.getName():"
+ file.getName());
System.out.println(
"file.getOriginalFilename():"
+ file.getOriginalFilename());
String folder =
"E:\\upload\\received\\"
;
String picName =
new
Date().getTime() +
".jpg"
;
File filelocal =
new
File(folder, picName);
result =
new
JSONObject();
result.put(picName, folder + picName);
try
{
file.transferTo(filelocal);
}
catch
(IOException e) {
e.printStackTrace();
}
}
JSONObject jsonObject =
new
JSONObject();
jsonObject.put(
"error_code"
,
223805
);
jsonObject.put(
"reason"
,
"文件过大或上传发生错误"
);
Random random =
new
Random();
if
(random.nextInt(
10
) >
3
) {
jsonObject.put(
"error_code"
,
0
);
jsonObject.put(
"reason"
,
"success"
);
jsonObject.put(
"result"
, result);
}
return
jsonObject;
}
/**
* 知道参数名的文件上传
*
* @param multipartFile 文件
* @return 返回
* @throws IOException
*/
@PostMapping
(
"/uploadCommon"
)
public
JSONObject upload(
@RequestParam
(
"A"
) MultipartFile multipartFile)
throws
IOException {
String name = multipartFile.getName();
String originalFilename = multipartFile.getOriginalFilename();
long
size = multipartFile.getSize();
String folder =
"E:\\upload\\received\\"
;
String picName =
new
Date().getTime() +
".jpg"
;
File filelocal =
new
File(folder, picName);
multipartFile.transferTo(filelocal);
JSONObject jsonObject =
new
JSONObject();
jsonObject.put(
"error_code"
,
223805
);
jsonObject.put(
"reason"
,
"文件过大或上传发生错误"
);
Random random =
new
Random();
if
(random.nextInt(
10
) >
3
) {
jsonObject.put(
"error_code"
,
0
);
jsonObject.put(
"reason"
,
"success"
);
JSONObject result =
new
JSONObject();
result.put(name, folder + picName);
jsonObject.put(
"result"
, result);
}
return
jsonObject;
}
}