2.x Multipart/form-data 服务器返回 400 Bad Request 和 MessageBodyWriterNotFoundException。

huangapple go评论71阅读模式
英文:

Jersey 2.x Multipart/form-data server returns 400 Bad Request and MessageBodyWriterNotFoundException

问题

以下是您提供的内容的翻译:

当我在Jersey 2.26中运行我的Multipart/form-data上传功能时,它返回400-Bad请求。我尝试了许多方法,但没有用。

当不上传文件时,它显示响应消息。当我使用客户端时,出现了 "MessageBodyWriterNotFoundException"。

Upload.java

@Path("/upload")
public class UploadFileDemo {
	
	@POST
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.TEXT_PLAIN)
	public Response uploadFile(@FormDataParam("file") FileInputStream fileInputStream,
								@FormDataParam("file") FormDataContentDisposition fileInfo) {
		System.out.println(fileInfo.getFileName() + "-" + fileInfo.getSize());
		String path = "D://" + fileInfo.getFileName();
		int read = 0;
		byte[] bytes = new byte[1024];
		try {
			OutputStream out = new FileOutputStream(new File(path));
			while((read = fileInputStream.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}
			out.flush();
			out.close();	
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Response.ok("数据上传成功").build();
	}
}

web.xml

<servlet>
	<servlet-name>Jersey Web Application</servlet-name>
	<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
	<init-param>
		<param-name>jersey.config.server.provider.packages</param-name>
		<param-value>com.packages.resource</param-value>
	</init-param>
	<init-param>
		<param-name>jersey.config.server.provider.classnames</param-name>
		<param-value>org.glassfish.jersey.filter.LoggingFilter;
		org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>Jersey Web Application</servlet-name>
	<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

FYI- 未显示任何异常

我的日志过滤器日志
Aug 25, 2020 12:53:29 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * 服务器在线程 http-nio-9090-exec-8 上收到了请求
1 > POST http://localhost:9090/rest-new-version/webapi/upload/file
1 > accept: */*
1 > accept-encoding: gzip, deflate, br
1 > connection: keep-alive
1 > content-length: 240
1 > content-type: multipart/form-data; boundary=--------------------------247816562830114154909314
1 > host: localhost:9090
1 > postman-token: 91bec8ef-5857-4604-a512-6dc97d5e22c6
1 > user-agent: PostmanRuntime/7.26.3

Aug 25, 2020 12:53:30 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * 服务器在线程 http-nio-9090-exec-8 上作出了响应
1 < 400

请帮助我解决这个问题 2.x Multipart/form-data 服务器返回 400 Bad Request 和 MessageBodyWriterNotFoundException。

英文:

When I run my Multipart/form-data uploading feature in jersey 2.26, it returns 400-Bad request. I triedno many ways but no use

when uploading without file, it shows the response message. when I use client "MessageBodyWriterNotFoundException" showed.

Upload.java

@Path(&quot;/upload&quot;)
public class UploadFileDemo {
	
	@POST
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.TEXT_PLAIN)
	public Response uploadFile(@FormDataParam(&quot;file&quot;) FileInputStream fileInputStream,
								@FormDataParam(&quot;file&quot;) FormDataContentDisposition fileInfo) {
		System.out.println(fileInfo.getFileName()+&quot;-&quot;+fileInfo.getSize());
		String path= &quot;D://&quot;+fileInfo.getFileName();
		int read=0;
		byte[] bytes = new byte[1024];
		try {
			OutputStream out = new FileOutputStream(new File(path));
			while((read = fileInputStream.read(bytes))!=-1) {
				out.write(bytes, 0, read);
			}
			out.flush();
			out.close();	
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Response.ok(&quot;Data uploaded successfully&quot;).build();
	}

}

web.xml

&lt;servlet&gt;
		&lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
		&lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
		&lt;init-param&gt;
			&lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
			&lt;param-value&gt;com.packages.resource&lt;/param-value&gt;
		&lt;/init-param&gt;
		&lt;init-param&gt;
			&lt;param-name&gt;jersey.config.server.provider.classnames&lt;/param-name&gt;
			&lt;param-value&gt;org.glassfish.jersey.filter.LoggingFilter;
			org.glassfish.jersey.media.multipart.MultiPartFeature&lt;/param-value&gt;
		&lt;/init-param&gt;
		&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
	&lt;/servlet&gt;
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/webapi/*&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;

FYI- No exception showed

My logging filter log
Aug 25, 2020 12:53:29 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Server has received a request on thread http-nio-9090-exec-8
1 &gt; POST http://localhost:9090/rest-new-version/webapi/upload/file
1 &gt; accept: */*
1 &gt; accept-encoding: gzip, deflate, br
1 &gt; connection: keep-alive
1 &gt; content-length: 240
1 &gt; content-type: multipart/form-data; boundary=--------------------------247816562830114154909314
1 &gt; host: localhost:9090
1 &gt; postman-token: 91bec8ef-5857-4604-a512-6dc97d5e22c6
1 &gt; user-agent: PostmanRuntime/7.26.3

Aug 25, 2020 12:53:30 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Server responded with a response on thread http-nio-9090-exec-8
1 &lt; 400

please help me to get through this 2.x Multipart/form-data 服务器返回 400 Bad Request 和 MessageBodyWriterNotFoundException。

答案1

得分: 0

@Path("/upload")
public class UploadFileDemo {
    
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
                                @FormDataParam("file") FormDataContentDisposition fileInfo) {
        System.out.println(fileInfo.getFileName() + "-" + fileInfo.getSize());
        String path = "D://" + fileInfo.getFileName();
        int read = 0;
        byte[] bytes = new byte[1024];
        try {
            OutputStream out = new FileOutputStream(new File(path));
            while ((read = fileInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response.ok("Data uploaded successfully").build();
    }
}

This will work.
Happy Coding!!!


<details>
<summary>英文:</summary>

FileInputStream Used as Parameter. SO it returned 400 Bad request. Instead of that use **InputStream**

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

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@FormDataParam(&quot;file&quot;) InputStream fileInputStream,
                            @FormDataParam(&quot;file&quot;) FormDataContentDisposition fileInfo) {
    System.out.println(fileInfo.getFileName()+&quot;-&quot;+fileInfo.getSize());
    String path= &quot;D://&quot;+fileInfo.getFileName();
    int read=0;
    byte[] bytes = new byte[1024];
    try {
        OutputStream out = new FileOutputStream(new File(path));
        while((read = fileInputStream.read(bytes))!=-1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Response.ok(&quot;Data uploaded successfully&quot;).build();
}

}


This will work.
Happy Coding!!!

</details>



huangapple
  • 本文由 发表于 2020年8月25日 15:41:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63574194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定