在Spring Boot應用程式中上傳圖像時遇到問題?別擔心! php小編百草為您提供解決方案。無論您遇到什麼上傳圖像的困難,都可以在這裡找到答案。繼續閱讀我們的java問答,掌握如何在Spring Boot應用程式中成功上傳圖像的方法。讓我們一起來解決這個問題吧!
我開發了一個 springboot 應用程序,使用 react 作為前端,將圖像上傳到資料夾中。
@postmapping(value = "/upload") public responseentity<?> uploadimage(@requestparam("user") string user, @requestparam("image") multipartfile file) { try { system.out.println("-------------------------------------------------------------------"); this.process(ioutils.tobytearray(file.getinputstream()), user, file.getoriginalfilename().substring(file.getoriginalfilename().lastindexof("."))); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return new responseentity<>("uploaded", httpstatus.ok);//userservice.uploadimagefile(user, file); } @async public void process(byte[] bs, string user, string ext) throws ioexception { string fname = user.substring(0, user.lastindexof("@")).concat(ext); // full path string filepath = path + file.separator + fname; system.out.println(fname + " and " + filepath); file f = new file(path); if (!f.exists()) { f.mkdir(); } file convertedfile = new file(filepath); fileoutputstream fos = new fileoutputstream(convertedfile); system.out.println("-----started------"); fos.write(bs); fos.close(); system.out.println("-----closed------"); }
我正在嘗試將圖像上傳到資料夾中。 使用 file.getinputstream 將圖像分段檔案上傳到資料夾時,出現下列錯誤
org.springframework.web.multipart.support.StandardServletMultipartResolver[0;39m: Failed to perform cleanup of multipart items java.io.UncheckedIOException: Cannot delete D:\JS Projects\myapp\BackEnd\UserLoginSignUp\images\work\Tomcat-2\localhost\ROOT\upload_d31827cc_51e6_4901_b60e_6afdd0ee8a99_00000004.tmp at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.delete(DiskFileItem.java:431) at org.apache.catalina.core.ApplicationPart.delete(ApplicationPart.java:53) at org.springframework.web.multipart.support.StandardServletMultipartResolver.cleanupMultipart(StandardServletMultipartResolver.java:134) at org.springframework.web.servlet.DispatcherServlet.cleanupMultipart(DispatcherServlet.java:1251) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1108) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:555) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:623) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:926) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1791) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833)
導致上述錯誤和異常的原因可能是什麼?我們如何解決上述問題?
考慮使用 files.copy()
,它將為您節省大量工作。
public void saveFile(String directory, MultipartFile file) { if (file.isEmpty()) { return; } String fileName = file.getOriginalFilename(); if (Objects.isNull(fileName) || fileName.isEmpty()) { fileName = UUID.randomUUID().toString(); } try { Path path = Paths.get(directory); Files.copy(file.getInputStream(), path.resolve(fileName)); } catch (Exception e) { throw new RuntimeException(e); } }
以上是無法在 Spring Boot 應用程式中上傳圖像的詳細內容。更多資訊請關注PHP中文網其他相關文章!