Jersey REST服务Multipart/form-data解析异常:应用端请求返回空内容
Got it, let's break down why your Jersey service is returning null for file content when called from your app—even though it works fine in SOAP UI. The core issue starts with how you're trying to access the file data in your code, but we'll cover all possible angles to fix this.
1. Fix the File Content Access Logic
Your current code is trying to pull content from the top-level multipart request's Content-Disposition header, which isn't where individual file parts live. That's why get(0).getEntity() returns null. Instead, you need to iterate through the BodyPart entries in the multipart request—each part represents a form field or uploaded file.
Here's the corrected code:
import javax.ws.rs.core.Response; import org.glassfish.jersey.media.multipart.BodyPart; import org.glassfish.jersey.media.multipart.Multipart; import java.io.InputStream; public Response solution(Multipart multipart) { int partCount = multipart.size(); // This should match the number of parts in your request (e.g., 2 for a file + a text field) for (BodyPart part : multipart.getBodyParts()) { // Check if this part is a file (adjust media types based on your use case) if (part.getMediaType().toString().contains("octet-stream") || part.getMediaType().toString().startsWith("text/")) { // For binary files (images, docs), get content as InputStream InputStream fileStream = part.getEntityAs(InputStream.class); // For text files, you can directly get it as a String: // String fileContent = part.getEntityAs(String.class); // Process your file content here } // If you need metadata like filename or form field name: String fileName = part.getContentDisposition().getFilename(); String fieldName = part.getContentDisposition().getName(); } return Response.ok().build(); }
2. Verify Your App's Request Format
Even though SOAP UI works, your application might be constructing the multipart request incorrectly. Double-check these details:
- Ensure the request header includes
Content-Type: multipart/form-data; boundary=YOUR_CUSTOM_BOUNDARY, whereYOUR_CUSTOM_BOUNDARYexactly matches the separator used in the request body. - For the file part, confirm it has the correct header:
Content-Disposition: form-data; name="your_file_field_name"; filename="example.txt" - Make sure the actual file content is being included in the request body—sometimes apps accidentally send an empty payload for the file part.
3. Confirm Jersey Multipart Configuration
You need to have the MultiPartFeature registered in your Jersey app to handle multipart requests properly. If you haven't set this up, that could cause parsing issues:
If using a ResourceConfig class:
import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.glassfish.jersey.server.ResourceConfig; public class MyJerseyApplication extends ResourceConfig { public MyJerseyApplication() { register(MultiPartFeature.class); // Register your other resources here } }
If using web.xml:
Add this init-param to your Jersey servlet configuration:
<servlet> <servlet-name>Jersey Servlet</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> </servlet>
4. Key Difference: Top-Level vs Part-Specific Headers
Remember this critical distinction:
multipart.getContentDisposition()= refers to the header for the entire multipart request (usually not useful for file content)part.getContentDisposition()= refers to the header for the individual file/form part (where you'll find filename, field name, etc.)
Start with fixing the code to access BodyPart entities directly—that's almost certainly the root cause of your null value. If it still doesn't work, debug the incoming request from your app to confirm the payload matches what SOAP UI sends.
内容的提问来源于stack exchange,提问作者Great Falcon

