我有一個上傳文件到服務器(Spring Boot)從Android改造的問題。改裝 - 春季圖片上傳
這是我在Spring Boot中的代碼。
@RestController
@RequestMapping("beongae/api/{version}/profile")
public class ProfileController {
@RequestMapping(value = "/upload/{name}", method = RequestMethod.POST)
public ApiMessasge uploadBasic(@PathVariable("name") String name,
@RequestPart("file") MultipartFile data) throws IOException {
ApiMessasge apiMessasge = new ApiMessasge();
System.out.println("start upload !!");
if (!data.isEmpty()) {
try {
byte[] bytes = data.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream("./profilepictures/" + new File(name + ".png"))
);
stream.write(bytes);
stream.close();
apiMessasge.setCode(1);
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
for (int i = 0; i < e.getStackTrace().length; i++) {
System.out.println(e.getStackTrace()[i].toString());
}
apiMessasge.setCode(-1);
}
}
return apiMessasge;
}
}
這是Android的
Uri resultUri = result.getUri();
File file = new File(resultUri.getPath());
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
這是APiService.class
@Multipart
@Headers("Content-Type:application/json")
@POST("/beongae/api/{version}/profile/upload/{name}")
Call<ApiMessasge> upload(@Path("version") int version, @Path("name") String fileName
, @Part MultipartBody.Part file);
,這是在春季啓動
錯誤信息2017年9月19日21:34:51.179 ERROR 22271 --- [NIO-8080-EXEC-2] oaccC [[[/] [DispatcherServlet的]:。Servlet.service()爲 在路徑[]的上下文中的servlet [dispatcherServlet]拋出異常 [請求處理失敗;嵌套的異常是 org.springframework.web.multipart.MultipartException:當前請求 是不是一個多部分請求]與根源 org.springframework.web.multipart.MultipartException:當前的請求不是在org.springframework多部分請求 。 web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:151) 〜[spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web .method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) 〜[spring-web-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.method.support。 InvocableHandlerMethod.getMethodArgumentValues(InvocableHandl (InvocableHandlerMethod.java:128)。 ) 〜[spring-web-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) 〜[彈簧webmvc-4.3.10.RELEASE.jar /:4.3.10.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) 〜[spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(Requires estMappingHandlerAdapter.java:738) 〜[spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java :85) 〜[spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) 〜[spring -webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) 〜[spring-webmvc-4.3.10。 RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 〜[spring-webmvc-4.3.10.RELEASE.jar!/:4.3 .10.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 〜[spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10。發佈] at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) 〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.springframework.web.servlet .FrameworkServlet.service(FrameworkServlet.java:846) 〜[spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java: 742) 〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 〜[tomcat-embed- core-8.5.16.jar!/:8.5.16] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.16.jar!/ :8.5.16] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 〜[tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at org.apache.catalina .core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain。 java:166) 〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) 〜[spring- web-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 〜[spring-web-4.3.10.RELEASE的.jar /:4.3.10.RELE ASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve。 java:198) 〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed -core-8.5.16.jar!/:8.5.16] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.16.jar!/ :16年5月8日] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina.valves。 ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5。 16.jar!/:8.5.16] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.coyote.Abstrac tProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868) [!Tomcat的嵌入核心-8.5.16.jar /:16年5月8日] 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1455) [Tomcat的embed- core-8.5.16.jar!/:8.5.16] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar! /:8.5.16] 位於java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor。)上的java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_131] java:624) [na:1.8.0_131] at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar! /:8.5.16] 在java.lang.Thread.run(Thread.java:748)NA:1.8.0_131]
但是,這是當我用郵差做得很好。
請告訴我這是什麼問題以及如何解決
[POST多重表單數據使用改造的可能的複製2.0包括圖像](https://stackoverflow.com/questions/34562950/post-multipart-form-data-using-retrofit-2-0-including-image) – Antoniossss