繁体   English   中英

Java RESTful文件上传问题

[英]Java RESTful file upload question

我现在正在学习 REST 和 Spring 并且我必须做一些启动项目,才能使用这些技术。 因此,我在学习了一些教程后制作了一个 RESTful 应用程序,但在将文件上传到服务时遇到了一些问题。

当我在 FileUpload.html 的上下文菜单中点击 eclipse 中的 Run On Server 选项时,它给了我一个 HTTP 状态 404 - 未找到。 我运行 html 文件,但找不到它。 我不明白为什么。 我不得不说其他操作,如@GET 工作正常。 因此,当我从浏览器访问某个 GET 方法的地址时,它可以工作。 所以如果你们中的一些人知道什么,请告诉我,因为我真的不明白。 谢谢

这是 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();

    }
}

这是示例 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>

这是我的项目的目录树:

在此处输入图像描述

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>

.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>

.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>

您是否收到一个日志条目说它加载了 GalleryResource? 此外,如果您将 @Path("/locations") 更改为使用 @GET 的类中唯一的东西,它会起作用吗? 所以把它改成@Path("/locationsUpload")。 我之前遇到过使用相同路径条目的多个文件的问题。

你确定这个:

http://localhost:8080/locations/upload

是正确的?

并且不应在资源路径之前包含项目的名称,例如:

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

?

它是 spring web 应用程序吗?

我失踪了:

  • spring web 配置文件(在大多数情况下是一个单独的文件,但有时包含在 applicationContext.xml 中)
  • web.xml的部分spring配置并启动

您是否真的尝试从 HTML 文件启动服务器? -- 我以前从未见过这个选项。 -- 我总是将应用程序添加到服务器并从“服务器”视图启动服务器(Eclipse Alt-3 +“服务器”)

暂无
暂无

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

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