我想上传用户个人资料照片。我使用以下代码。它成功上传照片并将其保存在数据库中,但最后会发生以下异常。我不知道为什么会这样。
FileUploadController
@RequestMapping(method = RequestMethod.POST, value = "/uploadUserProfilePhoto/{userId}", produces="application/json")
public @ResponseBody UserPhoto handleUserProfileFileUpload(Authentication authentication, @PathVariable("userId") Long userId,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty())
return photoService.createUserPhoto(file , userId);
else
throw new RuntimeException( "You failed to upload " + file.getOriginalFilename() + " because the file was empty");
}
uploadPhotoService
@Transactional
public UserPhoto createUserPhoto(MultipartFile file, long userId){
User user = userRepository.findOne(userId);
UserPhoto previousPhoto = userPhotoRepository.findByUserId(userId);
if(previousPhoto!=null){
deletePhoto(previousPhoto.getPath());
return updatePhotoWithFile(file, previousPhoto);
}
else{
UserPhoto userPhoto = new UserPhoto();
userPhoto.setUser(user);
userPhoto = userPhotoRepository.save(userPhoto);
user.setProfilePhoto(userPhoto);
try {
String extension = photoUtils.getExtension(file);
photoUtils.assertValidExtenstion(extension);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(staticPath + "userPhotos/" + userPhoto.getId() + extension)));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();
userPhoto.setPath("userPhotos/" + userPhoto.getId() + extension);
userPhotoRepository.save(userPhoto);
return userPhoto;
}
catch (Exception e) {
throw new RuntimeException( "You failed to upload " + file.getOriginalFilename() + " because of server error", e);
}
}
}
private UserPhoto updatePhotoWithFile(MultipartFile file, UserPhoto previousPhoto){
try {
String extension = photoUtils.getExtension(file);
photoUtils.assertValidExtenstion(extension);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(staticPath + "userPhotos/" + previousPhoto.getId() + extension)));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();
previousPhoto.setPath("userPhotos/" + previousPhoto.getId() + extension);
userPhotoRepository.save(previousPhoto);
return previousPhoto;
}
catch (Exception e) {
throw new RuntimeException( "You failed to upload " + file.getOriginalFilename() + " because of server error", e);
}
}
private void deletePhoto(String path)
{
Path pathOfFile=Paths.get(staticPath+path);
try {
Files.deleteIfExists(pathOfFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
例外
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method) ~[na:1.8.0_91]
at sun.nio.ch.SocketDispatcher.write(Unknown Source) ~[na:1.8.0_91]
at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source) ~[na:1.8.0_91]
at sun.nio.ch.IOUtil.write(Unknown Source) ~[na:1.8.0_91]
at sun.nio.ch.SocketChannelImpl.write(Unknown Source) ~[na:1.8.0_91]
at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:134) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.tomcat.util.net.NioBlockingSelector.write(NioBlockingSelector.java:101) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:157) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.doWrite(NioEndpoint.java:1241) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.tomcat.util.net.SocketWrapperBase.writeBlocking(SocketWrapperBase.java:355) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.tomcat.util.net.SocketWrapperBase.write(SocketWrapperBase.java:324) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.coyote.http11.Http11OutputBuffer$SocketOutputBuffer.doWrite(Http11OutputBuffer.java:580) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:112) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.coyote.http11.Http11OutputBuffer.doWrite(Http11OutputBuffer.java:213) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.coyote.Response.doWrite(Response.java:502) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:375) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
... 100 common frames omitted