我有一个可以进行文件上传的API。在我的api中,文件上传应该是可选的。如果用户请求没有文件和数据,API应该接受请求并将数据保存到DB。我试过如下:
@Transactional
@RequestMapping(value = "/bugg-report", method = RequestMethod.POST)
public @ResponseBody
UserResponse fileUploaded(@RequestParam(required = true, value = "user_emailId") String email, @RequestParam(required = true, value = "category_id") String categoryId, @RequestParam(required = true, value = "subject") String subject, @RequestParam(required = true, value = "description") String description,
@RequestParam(required = false, value = "uploadedFile") MultipartFile file) throws SQLException, ClassNotFoundException, IOException {
String status = "Failed";
BugReport bugReport = null;
UserResponse userResponse = new UserResponse();
User user = null;
int bugId = 0;
try {
user = loginService.getUser(email);
if (user != null) {
int userId = user.getProfileId();
bugId = userDao.getLastBugReportId() + 1;
bugReport = new BugReport();
bugReport.setUserId(userId);
bugReport.setCategoryId(Integer.parseInt(categoryId));
bugReport.setBugSubject(subject);
bugReport.setBugDescription(description);
bugReport.setReportedDate(new Date());
bugReport.setCurrentStatus(1);
bugReport.setLastUpdatedDate(new Date());
bugReport.setLastUpdatedUser(userId);
InputStream inputStream = null;
OutputStream outputStream = null;
//MultipartFile file = uploadedFile.getFile();
if (!file.isEmpty()) {
// fileValidator.validate(uploadedFile, result);
String originalFileName = file.getOriginalFilename();
System.out.println("originalFileName = " + originalFileName);
// if (result.hasErrors()) {
// userResponse.setCode(WeekenterConstants.FAILURE_CODE);
// userResponse.setMessage("Failed try again");
// return userResponse;
// }
try {
inputStream = file.getInputStream();
String extension = "png";//Default
if (originalFileName != null) {
if (!originalFileName.equals("")) {
extension = Util.getExtension(originalFileName);
}
}
String filename = bugId + "." + extension;
File newFile = new File(WeekenterConstants.IMAGES_FOLDER);
if (!newFile.exists()) {
newFile.mkdirs();
}
outputStream = new FileOutputStream(newFile + "/" + filename);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
inputStream.close();
outputStream.close();
bugReport.setImageName(filename);
userResponse.setCode(WeekenterConstants.SUCCESS_CODE);
userResponse.setMessage("Success");
userResponse.setEmail(email);
userResponse.setId(bugId);
} catch (IOException e) {
e.printStackTrace();
userResponse.setCode(WeekenterConstants.FAILURE_CODE);
userResponse.setEmail(email);
userResponse.setMessage("Failed try again");
}
}
} else {
userResponse.setCode(WeekenterConstants.FAILURE_CODE);
userResponse.setMessage("Invalid email id");
}
} catch (Exception e1) {
userResponse.setCode(WeekenterConstants.FAILURE_CODE);
userResponse.setMessage("Failed try again");
}
if (bugReport != null) {
bugId = userDao.saveBugReport(bugReport);
userResponse.setCode(WeekenterConstants.SUCCESS_CODE);
userResponse.setEmail(email);
userResponse.setMessage("Success");
userResponse.setId(bugId);
}
return userResponse;
}
但如果我请求没有文件就抛出
请求处理失败;嵌套异常是 org.springframework.web.multipart.MultipartException:当前 请求不是多部分请求
而且我还需要知道如何发送JSON请求而不是传递参数?