Я пытаюсь реализовать код для загрузки файлов изображений с локального компьютера на сервер, и у меня есть некоторые проблемы. Происходит что-то странное, когда я впервые пытался загрузить файл, он работал, он работал должным образом, я был так счастлив, что сделал это .... но в следующий раз, когда я попробовал это, это выдает ошибку ... Заранее спасибо, ребята ... пожалуйста, помогите мне
это мой код для этой функции
package kr.or.connect.booking.controller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import kr.or.connect.booking.dto.Comment;
import kr.or.connect.booking.dto.CommentImg;
import kr.or.connect.booking.dto.ImgFile;
import kr.or.connect.booking.service.CommentImgService;
import kr.or.connect.booking.service.CommentService;
import kr.or.connect.booking.service.ImgFileService;
import kr.or.connect.booking.service.ReservationInfoService;
import kr.or.connect.booking.util.SessionCheck;
@Controller
public class CommentController {
@Autowired
CommentImgService cmntImgService;
@Autowired
CommentService cmntService;
@Autowired
ImgFileService imgFileService;
@Autowired
ReservationInfoService rsvInfoService;
private Logger logger = LoggerFactory.getLogger(this.getClass());
private SessionCheck sessionChecker = new SessionCheck();
@PostMapping("/comment")
public String comment(@RequestParam("file")MultipartFile file, HttpServletRequest request ) {
String content = request.getParameter("reviewContent");
int rating = Integer.parseInt(request.getParameter("rating"));
int reservationInfoId = Integer.parseInt(request.getParameter("reservationInfoId"));
int productId = Integer.parseInt(request.getParameter("productId"));
String fileName = file.getOriginalFilename();
String saveFileName = "e:/tmp/img/"+file.getOriginalFilename();
String contentType= file.getContentType();
logger.debug("리뷰내용 : {}",content);
logger.debug("rating : {}",rating);
logger.debug("파일 크기 : {}",file.getSize());
logger.debug("파일 이름 : {}" ,fileName);
logger.debug("파일 저장이름 : {}" , saveFileName);
try(
// 맥일 경우
//FileOutputStream fos = new FileOutputStream("/tmp/" + file.getOriginalFilename());
// 윈도우일 경우
FileOutputStream fos = new FileOutputStream("img/" + file.getOriginalFilename());
InputStream is = file.getInputStream();
){
int readCount = 0;
byte[] buffer = new byte[1024];
while((readCount = is.read(buffer)) != -1){
fos.write(buffer,0,readCount);
}
}catch(Exception ex){
throw new RuntimeException("file Save Error");
}
Comment cmnt = new Comment();
cmnt.setComment(content);
cmnt.setProductId(productId);
cmnt.setScore(rating);
cmnt.setReservationInfoId(reservationInfoId);
int cmntId = cmntService.insertCmnt(cmnt);
ImgFile imgfile = new ImgFile();
imgfile.setContent_type(contentType);
imgfile.setFile_name(fileName);
imgfile.setSave_file_name(saveFileName);
imgfile.setDelete_flag(0);
int fileId = imgFileService.uploadFile(imgfile);
CommentImg cmntImg = new CommentImg();
cmntImg.setFileId(fileId);
cmntImg.setReservationInfoId(reservationInfoId);
cmntImg.setReservationUserCommentId(cmntId);
cmntImgService.insert(cmntImg);
return "redirect:myreservation";
}
и это ошибка, которую я получил.
: 2월 26, 2020 10:34:52 오후 org.apache.catalina.core.StandardWrapperValve invoke
심각: Servlet.service() for servlet [mvc] in context with path [/booking] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: file Save Error] with root cause
java.lang.RuntimeException: file Save Error
at kr.or.connect.booking.controller.CommentController.comment(CommentController.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)