在上传文件jersey webservice中使用Multipart时出错

时间:2014-06-10 15:36:24

标签: java upload jersey multipart

我正在使用此代码上传文件

@Path(value= "/up")
public class upload {
private String SERVER_UPLOAD_LOCATION_FOLDER= "C://Users/Marya/Desktop/" ;
@POST
@Path(value="upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(
@FormDataParam("file") InputStream fileInputStream , @FormDataParam("name") String name  ) {String filePath = SERVER_UPLOAD_LOCATION_FOLDER + name ;
    System.out.println("*****serverpath********");
    saveFile(fileInputStream, filePath);
    String output = "File saved to server location : " + filePath;
            System.out.println("done");
            return output;}
private void saveFile(InputStream uploadedInputStream,String serverLocation) {
try {OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
int read = 0;
byte[] bytes = new byte[1024];
        outpuStream = new FileOutputStream(new File(serverLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            outpuStream.write(bytes, 0, read);
            System.out.println("*****read********");
        }
        outpuStream.flush();
        outpuStream.close();
    } catch (IOException e) {e.printStackTrace();}

似乎我的multipart.jar有问题,因为当我使用mutipart-1.18.jar时,文件被上传但是它是空的!!当我改为multipart-1.8.jar时,我得到了这个输出

File saved to server location : C://Users/Marya/Desktop/------    WebKitFormBoundaryxR9egGKFn3cgmnSz
Content-Disposition: form-data; name="name"

name 
------WebKitFormBoundaryxR9egGKFn3cgmnSz
Content-Disposition: form-data; name="sms.PNG"; filename="sms.PNG"
Content-Type: image/png

这个错误!!!

java.io.FileNotFoundException: Invalid file path
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.example.upload.saveFile(upload.java:92)
at com.example.upload.uploadFile(upload.java:80)
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 com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

任何人都可以帮我解决这个问题PLZ !!

1 个答案:

答案 0 :(得分:0)

您确定C://Users/Marya/Desktop是计算机上的有效路径吗?

它看起来像Windows路径,但Windows使用反斜杠作为路径分隔符。

请尝试使用反斜杠。你可能需要逃脱它们。

  

C:\\用户\玛利亚\桌面

这是一个关于在Java中构建文件路径的好资源

http://www.mkyong.com/java/how-to-construct-a-file-path-in-java/