开发者

Java RESTful file upload question

I am learning now REST and Spring and I have to do some starting project, to get used with the tech开发者_StackOverflownologies. So, I made a RESTful application after some tutorials and I have some problems with uploading files to the service.

When I hit the Run On Server option in eclipse in the context menu of FileUpload.html, it gives me a HTTP Status 404 - Not Found. I run the html file and it can't find it. I don't understand why. I have to say that other actions, like @GET are working properly. So when I access from the browser some address for a GET method, it works. So if some o f you know something, please let me know because I really don't get it. Thanks

Here is the GalleryResource class:

@Path("/locations")
public class GalleryResource {
    private GalleryService galleryService = new PicturesGalleryService();

    @POST
    @Path("/upload")
    @Consumes("multipart/form-data")
    public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {

        String newFile= "c:/gallery/"
                + fileDetail.getFileName();

        FileUtiles.createNewPicture(newFile);

        String output = "File uploaded to : " + uploadedFileLocation;

        return Response.status(200).entity(output).build();

    }
}

Here is the sample web page:

<html>
<body> 
    <form action="http://localhost:8080/locations/upload" method="post" enctype="multipart/form-data">

      <input type="file" name="file"/> 
      <input type="submit" value="Upload File" />
    </form> 
</body>
</html>

Here is the directory tree of my project:

Java RESTful file upload question

The web.xml file:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.rest.sample.resources</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

The .project file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="build/classes" path="src/main/java"/>
    <classpathentry kind="src" output="build/resource" path="src/main/resources"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="output" path="build/classes"/>
</classpath>

The .classpath file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="build/classes" path="src/main/java"/>
    <classpathentry kind="src" output="build/resource" path="src/main/resources"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="output" path="build/classes"/>
</classpath>


Do you get a log entry saying that it loaded GalleryResource? Also, if you change your @Path("/locations") to something unique from the classes that use @GET does it work? So change it to @Path("/locationsUpload"). I've had issues before with multiple files using the same path entry.


Are you sure this:

http://localhost:8080/locations/upload

is correct?

And should not include the name of the your project before the resource path, like:

http://localhost:8080/RESTservice/locations/upload

?


Is it a spring web application?

I am missing:

  • the spring web configuration file (in most cases a seperate file but sometimes included in applicationContext.xml)
  • The part of the web.xml where spring is configured and started

Did you really tryed to start the server from the HTML file? -- I never have seen this option before. -- I always add the application to the Server and start the Server from the "Server" view (Eclipse Alt-3 + "Server")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜