您正在使用spring mvc和rest web services将多个文件上传到aws存储桶中。 积极的情况就像是如果我选择更多的一个文件,它保存在aws桶中,并在这里得到200 String json1 = handler.handleResponse(response1); 的System.out.println(json1);
我的问题是我选择了三个名为x,y和z的文件,因为一些问题y和z文件无法保存如何通知用户y和z未保存,因此第一个文件被保存到存储桶中进入桶
@PostMapping("/upload")
public String handleFileUpload(@RequestParam("specifications") MultipartFile[] specifications,
HttpServletRequest request,HttpSession session,final RedirectAttributes redirectAttributes) throws Exception {
for (int i = 0; i < specifications.length; i++) {
MultipartFile file = specifications[i];
String path = "Specification/";
String bucketName="BUcket/";
String inJson = "{\"filename\":\"" + file.getOriginalFilename() + "\",\"bucketname\":\""+ bucketName + "\",\"path\":\""+ path + "\"}";
addLogo(file, inJson);
}
代码上传文件
public void addLogo(MultipartFile file ,String inJson) throws IOException
{
String message="";
byte[] bytes = file.getBytes();
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httppost = new HttpPost(fileUploadURL);
HttpEntity entity = MultipartEntityBuilder.create().addTextBody("json", inJson).addBinaryBody("file", bytes).build();
httppost.setEntity(entity);
HttpResponse response1 = httpclient.execute(httppost);
System.out.print(response1.getStatusLine());
ResponseHandler<String> handler = new BasicResponseHandler();
String json1 = handler.handleResponse(response1);
System.out.println(json1);
message = message + "You successfully uploaded " + file.getOriginalFilename() + "\n";
System.out.println(message);
}
答案 0 :(得分:1)
通过使用ResponseEntity spring对象,您可以自定义基于返回的上传结果。你捕获IOEXception并创建一个特定的返回String, 我修改你的方法是这样的:
@PostMapping("/upload")
public ResponseEntity<?> handleFileUpload(@RequestParam("specifications")
MultipartFile[] specifications,
HttpServletRequest request,HttpSession session,final RedirectAttributes
redirectAttributes) throws Exception {
String failed_upload="";
for (int i = 0; i < specifications.length; i++) {
try{
MultipartFile file = specifications[i];
String path = "Specification/";
String bucketName="BUcket/";
String inJson = "{\"filename\":\"" + file.getOriginalFilename()
+ "\",\"bucketname\":\""+ bucketName + "\",\"path\":\""+ path + "\"}";
addLogo(file, inJson);
}catch(IOException){
failed_upload=failed_upload+specifications[i]+" ,";
}
} if(!failed_upload.equals("")){
return new ResponseEntity<>("Files"+failed_upload+" not uploaded",
HttpStatus.INTERNAL_SERVER_ERROR);
}else{
return new ResponseEntity<>("Everything is ok", HttpStatus.OK);
}