Tidak dapat memuat naik imej dalam aplikasi Spring Boot
Mengalami masalah memuat naik imej dalam aplikasi Spring Boot anda? jangan risau! Editor PHP Baicao memberi anda penyelesaian. Tidak kira apa kesukaran yang anda hadapi memuat naik imej, anda akan temui jawapannya di sini. Teruskan membaca Soal Jawab java kami untuk mengetahui cara berjaya memuat naik imej dalam aplikasi Spring Boot. Mari kita selesaikan masalah ini bersama-sama!
Kandungan soalan
Saya membangunkan aplikasi springboot menggunakan react sebagai bahagian hadapan untuk memuat naik imej ke dalam folder.
@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------"); }
Saya cuba memuat naik imej ke dalam folder. Apabila memuat naik fail tersegmen imej ke folder menggunakan file.getinputstream, saya mendapat ralat berikut
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)
Apakah kemungkinan sebab ralat dan pengecualian di atas? Bagaimana kita menyelesaikan masalah di atas?
Penyelesaian
Pertimbangkan untuk menggunakan files.copy()
, ia akan menjimatkan banyak kerja anda.
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); } }
Atas ialah kandungan terperinci Tidak dapat memuat naik imej dalam aplikasi Spring Boot. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

