简体   繁体   English

Java RESTful文件上传问题

[英]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 technologies.我现在正在学习 REST 和 Spring 并且我必须做一些启动项目,才能使用这些技术。 So, I made a RESTful application after some tutorials and I have some problems with uploading files to the service.因此,我在学习了一些教程后制作了一个 RESTful 应用程序,但在将文件上传到服务时遇到了一些问题。

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.当我在 FileUpload.html 的上下文菜单中点击 eclipse 中的 Run On Server 选项时,它给了我一个 HTTP 状态 404 - 未找到。 I run the html file and it can't find it.我运行 html 文件,但找不到它。 I don't understand why.我不明白为什么。 I have to say that other actions, like @GET are working properly.我不得不说其他操作,如@GET 工作正常。 So when I access from the browser some address for a GET method, it works.因此,当我从浏览器访问某个 GET 方法的地址时,它可以工作。 So if some of you know something, please let me know because I really don't get it.所以如果你们中的一些人知道什么,请告诉我,因为我真的不明白。 Thanks谢谢

Here is the GalleryResource class:这是 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:这是示例 web 页面:

<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:这是我的项目的目录树:

在此处输入图像描述

The web.xml file: web.xml 文件:

<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: .project 文件:

<?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: .classpath 文件:

<?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?您是否收到一个日志条目说它加载了 GalleryResource? Also, if you change your @Path("/locations") to something unique from the classes that use @GET does it work?此外,如果您将 @Path("/locations") 更改为使用 @GET 的类中唯一的东西,它会起作用吗? So change it to @Path("/locationsUpload").所以把它改成@Path("/locationsUpload")。 I've had issues before with multiple files using the same path entry.我之前遇到过使用相同路径条目的多个文件的问题。

Are you sure this:你确定这个:

http://localhost:8080/locations/upload 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 http://localhost:8080/RESTservice/locations/upload

? ?

Is it a spring web application?它是 spring web 应用程序吗?

I am missing:我失踪了:

  • the spring web configuration file (in most cases a seperate file but sometimes included in applicationContext.xml) spring web 配置文件(在大多数情况下是一个单独的文件,但有时包含在 applicationContext.xml 中)
  • The part of the web.xml where spring is configured and started web.xml的部分spring配置并启动

Did you really tryed to start the server from the HTML file?您是否真的尝试从 HTML 文件启动服务器? -- 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") -- 我总是将应用程序添加到服务器并从“服务器”视图启动服务器(Eclipse Alt-3 +“服务器”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM