Springboot受信ファイル
4195 ワード
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 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(MultipartFile multipartFile) throws IOException {
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);
/* {
"reason": "success",
"result": {
"D": "/upload/order/files/2016/a72750ad-8950-4949-b04a-37e69aff0d23.jpg",
"A": "/upload/order/files/2016/6842811a-eb76-453b-a2f3-488e2bb4500e.jpg",
"B": "/upload/order/files/2016/ccc96347-3cb8-4e2e-99a3-0c697b57eb88.jpg",
"C": "/upload/order/files/2016/d470d533-a54b-406a-a0f9-bbf82c314755.jpg"
},
"error_code": 0
}*/
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;
}
}