You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jersey上传文件时出现404资源未找到错误,求排查解决

Troubleshooting 404 Error for Jersey File Upload Service

Hey there! Let's work through this 404 issue you're hitting with your Jersey file upload service. I'll walk through the most common culprits and how to validate your setup step by step:

1. Fix Up Your web.xml Configuration

Your provided web.xml snippet cuts off, so let's make sure the core Jersey setup is correct—this is often where 404s start. A proper config for file uploads needs three key pieces:

  • The Jersey servlet class (for Jersey 2.x, that's org.glassfish.jersey.servlet.ServletContainer)
  • A URL pattern that matches your upload endpoint's base path
  • Registration of the multi-part feature (mandatory for handling file uploads)

Here's a working example to compare against:

<servlet>
    <servlet-name>Jersey File Upload</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey File Upload</servlet-name>
    <url-pattern>/api/*</url-pattern> <!-- This must align with your resource's @Path -->
</servlet-mapping>

Double-check that your upload resource's @Path annotation plays well with this mapping. For example, if your servlet maps to /api/*, a resource with @Path("/upload") will have a full endpoint of http://your-domain/api/upload.

2. Verify Your JSP Form's Action Path

The form in WelcomeFile.jsp must point exactly to your Jersey endpoint—typos or missing context here are a super common cause of 404s. If your endpoint is /api/upload, your form should look like this:

<form method="POST" action="api/upload" enctype="multipart/form-data">
    <input type="file" name="uploadedFile"/>
    <input type="submit" value="Upload File"/>
</form>

Pro tip: If your app is deployed with a context path (like http://localhost:8080/my-app/), use the JSP context path variable to make the action dynamic:
<form action="${pageContext.request.contextPath}/api/upload" ...>

3. Ensure Multi-Part Dependencies Are Installed

Jersey can't handle file uploads without the jersey-media-multipart module. If you're using Maven, confirm this is in your pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>your-jersey-version</version> <!-- Match your core Jersey version exactly -->
</dependency>

Without this dependency, Jersey won't recognize multipart requests, which can manifest as a 404 even if your path is correct.

4. Validate Your Resource Class Setup

Your upload resource needs the right annotations to handle the file request. Here's a minimal working example to check against:

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.media.multipart.FormDataParam;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

@Path("/upload")
public class FileUploadResource {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String handleFileUpload(
            @FormDataParam("uploadedFile") InputStream fileStream,
            @FormDataParam("uploadedFile") org.glassfish.jersey.media.multipart.FormDataContentDisposition fileDetails) {
        
        try {
            String savePath = "your-target-directory/" + fileDetails.getFileName();
            FileOutputStream outStream = new FileOutputStream(new File(savePath));
            
            int readBytes;
            byte[] buffer = new byte[1024];
            while ((readBytes = fileStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, readBytes);
            }
            
            outStream.close();
            return "File uploaded successfully!";
        } catch (Exception e) {
            return "Upload failed: " + e.getMessage();
        }
    }
}

*Make sure:

  • The @Path value matches what's in your form action
  • @Consumes(MediaType.MULTIPART_FORM_DATA) is present
  • The @FormDataParam name matches the name attribute of your file input in the JSP*

5. Turn On Debug Logging

If you're still stuck, enable Jersey's tracing logs to see exactly what's happening with incoming requests. Add these init params to your web.xml:

<init-param>
    <param-name>jersey.config.server.tracing.type</param-name>
    <param-value>ALL</param-value>
</init-param>
<init-param>
    <param-name>jersey.config.server.tracing.threshold</param-name>
    <param-value>VERBOSE</param-value>
</init-param>

The logs will show if the request is reaching Jersey, if it's matching your endpoint, or if there's a hidden issue like missing features.

If you can share your full web.xml and resource class code, we can narrow this down even more quickly!

内容的提问来源于stack exchange,提问作者Pankaj Dagar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:29:33