关于MULTIPART_FORM_DATA:找不到类型为publicjavax.ws.rs.core.Response的参数的注入源的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Ajax
关于MULTIPART_FORM_DATA:找不到类型为public javax.ws.rs.core.Response的参数的注入源的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Ajax 提交 form ENCTYPE="multipart/form-data" 的方法、application / x-www-form-urlencoded或multipart / form-data?、application/x-www-form-urlencode/multipart/form-data、application/x-www-form-urlencoded or multipart/form-data?等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- MULTIPART_FORM_DATA:找不到类型为public javax.ws.rs.core.Response的参数的注入源
- Ajax 提交 form ENCTYPE="multipart/form-data" 的方法
- application / x-www-form-urlencoded或multipart / form-data?
- application/x-www-form-urlencode/multipart/form-data
- application/x-www-form-urlencoded or multipart/form-data?
MULTIPART_FORM_DATA:找不到类型为public javax.ws.rs.core.Response的参数的注入源
我正在使用基于Jersey的Restful Service实施策略来构建将用于上传文件的服务。我的服务类名称是:UploadFileService.java(请参见下面的代码)
package com.jerser.service;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import com.sun.jersey.core.header.FormDataContentDisposition;import com.sun.jersey.multipart.FormDataParam;@Path("/fileUpload")public class UploadFileService { @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) { String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName(); // save it writeToFile(uploadedInputStream, uploadedFileLocation); String output = "File uploaded to : " + uploadedFileLocation; return Response.status(200).entity(output).build(); } // save uploaded file to new location private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) { try { OutputStream out = new FileOutputStream(new File( uploadedFileLocation)); int read = 0; byte[] bytes = new byte[1024]; out = new FileOutputStream(new File(uploadedFileLocation)); while ((read = uploadedInputStream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }}
这些是我在lib中拥有的JAR文件:
aopalliance-repackaged-2.4.0-b10.jar asm-debug-all-5.0.2.jar hk2-api-2.4.0-b10.jar hk2-locator-2.4.0-b10.jar hk2-utils-2.4.0-b10.jar javassist-3.18.1-GA.jar javax.annotation-api-1.2.jar javax.inject-2.4.0-b10.jar javax.servlet-api-3.0.1.jar javax.ws.rs-api-2.0.1.jar jaxb-api-2.2.7.jar jersey-client.jar jersey-common.jar jersey-container-servlet-core.jar jersey-container-servlet.jar jersey-core-1.11.jar jersey-guava-2.17.jar jersey-media-jaxb.jar jersey-multipart-1.18.jar jersey-server.jarorg.osgi.core-4.2.0.jarosgi-resource-locator-1.0.1.jar persistence-api-1.0.jar validation-api-1.1.0.Final.jar
尝试启动tomcat服务器时出现以下错误:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.jerser.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; source=’ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.jerser.service.UploadFileService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@d3e2d4]}, definitionMethod=public javax.ws.rs.core.Response com.jerser.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}’]
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:528)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:166)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:327)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.
at org.glassfish.jersey.servlet.WebComponent.
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:171)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:363)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1176)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1009)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4885)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5212)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5207)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
在互联网上,我发现有很多示例,展示了如何使用RESTFul API上传MULTIPART文件。但是有相同的解决方案。我也无法运行这些代码。我认为我对JAR文件做错了。有人可以帮我吗?
答案1
小编典典摆脱jersey-multipart-1.18.jar
。就是说Jersey1.x。加上这两个
jersey-media-multipart-2.17
mimepull 1.9.3
对于Maven,你将使用以下依赖关系(你无需显式添加mimepull依赖关系,因为该依赖关系会将其引入)。
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.17</version> <!-- Make sure the Jersey version matches the one you are currently using --></dependency>
然后,你需要注册MultiPartFeature。如果你使用ResourceConfigfor进行配置,则只需执行
register(MultiPartFeature.class);
如果你使用的是web.xml,则可以将类作为添加
<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value></init-param>
请注意,如果你要注册多个提供程序,则可以用逗号或分号分隔每个提供程序类。你不能重复使用param-name两次。见Suarabh的回答
UPDATE
同样,一旦摆脱了,jersey-multipart-1.18.jar
你将为缺少的导入类产生编译错误。在大多数情况下,类名称仍然相同,只是包已更改,即
org.glassfish.jersey.media.multipart.FormDataParam
org.glassfish.jersey.media.multipart.FormDataContentDisposition
Aside
如果你要使用其他格式ModelValidationException,则这里有一些链接,以获取有关其他导致异常的信息。
- 1
- 2
- 3
Ajax 提交 form ENCTYPE="multipart/form-data" 的方法
转载:通过Ajax方式上传文件,使用FormData进行Ajax请求 (http://yunzhu.iteye.com/blog/2177923 )
application / x-www-form-urlencoded或multipart / form-data?
问题:
In HTTP there are two ways to POST data: application/x-www-form-urlencoded
and multipart/form-data
. 在HTTP中有两种POST数据的方式: application/x-www-form-urlencoded
和multipart/form-data
。 I understand that most browsers are only able to upload files if multipart/form-data
is used. 据我所知,如果使用multipart/form-data
,大多数浏览器只能上传文件。 Is there any additional guidance when to use one of the encoding types in an API context (no browser involved)? 在API上下文中使用其中一种编码类型时是否有任何其他指导(不涉及浏览器)? This might eg be based on: 这可能基于:
- data size 数据大小
- existence of non-ASCII characters 存在非ASCII字符
- existence on (unencoded) binary data 存在于(未编码的)二进制数据上
- the need to transfer additional data (like filename) 需要传输额外的数据(如文件名)
I basically found no formal guidance on the web regarding the use of the different content-types so far. 到目前为止,我基本上没有在网上找到有关使用不同内容类型的正式指导。
解决方案:
参考一: https://stackoom.com/question/Goef/application-x-www-form-urlencoded或multipart-form-data参考二: https://oldbug.net/q/Goef/application-x-www-form-urlencoded-or-multipart-form-data
application/x-www-form-urlencode/multipart/form-data
首先我们先认识下今天的application/x-www-form-urlencode/multipart/form-data属性所在的位置
1、form所属
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。 例如: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,这个一般文件上传时用。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
2、不同的编码方式
form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。
当数据传递的方式是get的时候,浏览器使用application/x-www-form-urlencode的编码方式,把form数据转换为一个字符串,如(name=zhangsan&age=128),然后把这个字符添加到url后面,用?连接 组成新的url并加载
当数据传递的方式是post的时候 浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。(ps:第二种方式的解释本人还有些模糊,有更为精确答案的猿,希望分享下)
application/x-www-form-urlencoded or multipart/form-data?
TL;DR
The moral of the story is, if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use multipart/form-data
. Otherwise, use application/x-www-form-urlencoded
.
The MIME types you mention are the two Content-Type
headers for HTTP POST requests that user-agents (browsers) must support. The purpose of both of those types of requests is to send a list of name/value pairs to the server. Depending on the type and amount of data being transmitted, one of the methods will be more efficient than the other. To understand why, you have to look at what each is doing under the covers.
For application/x-www-form-urlencoded
, the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&
), and names are separated from values by the equals symbol (=
). An example of this would be:
MyVariableOne=ValueOne&MyVariableTwo=ValueTwo
According to the specification:
[Reserved and] non-alphanumeric characters are replaced by `%HH'', a percent sign and two hexadecimal digits representing the ASCII code of the character
That means that for each non-alphanumeric byte that exists in one of our values, it''s going to take three bytes to represent it. For large binary files, tripling the payload is going to be highly inefficient.
That''s where multipart/form-data
comes in. With this method of transmitting name/value pairs, each pair is represented as a "part" in a MIME message (as described by other answers). Parts are separated by a particular string boundary (chosen specifically so that this boundary string does not occur in any of the "value" payloads). Each part has its own set of MIME headers like Content-Type
, and particularly Content-Disposition
, which can give each part its "name." The value piece of each name/value pair is the payload of each part of the MIME message. The MIME spec gives us more options when representing the value payload -- we can choose a more efficient encoding of binary data to save bandwidth (e.g. base 64 or even raw binary).
Why not use multipart/form-data
all the time? For short alphanumeric values (like most web forms), the overhead of adding all of the MIME headers is going to significantly outweigh any savings from more efficient binary encoding.
今天关于MULTIPART_FORM_DATA:找不到类型为public javax.ws.rs.core.Response的参数的注入源的讲解已经结束,谢谢您的阅读,如果想了解更多关于Ajax 提交 form ENCTYPE="multipart/form-data" 的方法、application / x-www-form-urlencoded或multipart / form-data?、application/x-www-form-urlencode/multipart/form-data、application/x-www-form-urlencoded or multipart/form-data?的相关知识,请在本站搜索。
本文标签: