簡體   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