gpt4 book ai didi

java - 上传文件时未找到 public javax.ws.rs.core.Response 类型参数的注入(inject)源

转载 作者:行者123 更新时间:2023-12-05 07:13:55 24 4
gpt4 key购买 nike

我想将文件从 ReactJS 上传到 Java 后端。但是它会在我上传的每个文件上添加这个:

------WebKitFormBoundaryu43OXGtsQPsxs0nM
Content-Disposition: form-data; name="file"; filename="lolo.png"
Content-Type: image/png

我再也打不开了。

然后我尝试使用 @FormDataParam 但在部署我的应用程序时出现此错误:

No injection source found for a parameter of type public javax.ws.rs.core.Response

我已经尝试添加这些依赖项:

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.30</version>
</dependency>
<dependency>
<groupId>org.jvnet.mimepull</groupId>
<artifactId>mimepull</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>

这是我的前端函数:

const formData = new FormData();
formData.append('file', file);

axios.post("/pj/"+incident.id +"/"+ file.name, formData);

这是我的后端函数:

@Path("/pj/{idIncident}/{fileName}")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@javax.ws.rs.core.Context HttpHeaders headers, @NotNull @PathParam("idIncident") Integer idIncident, @PathParam("fileName") String fileName, @FormDataParam("file") InputStream file){
try {
File theDir = new File("./myFolder/");
if (!theDir.exists()) {
theDir.mkdir();
}
String target = "./myFolder/"+fileName ;
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(target));
while ((read = file.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();

这是我的 web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Pilotage_rest_service</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!--<servlet>
<description>Jersey REST Service - PILOTAGE</description>
<servlet-name>Jersey REST Service PILOTAGE</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>pilotage.rest.Service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service PILOTAGE</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>-->

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>pilotage.rest</param-value>
</init-param>
<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 REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>


<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>PilotageRealm</realm-name>
</login-config>

<security-constraint>
<web-resource-collection>
<web-resource-name>Secure</web-resource-name>
<!-- be specific about the urls and methods here -->
<url-pattern>/Pilotage_WS/*</url-pattern>
</web-resource-collection>

<auth-constraint>
<description>has to be a valid user</description>
<role-name>*</role-name>
</auth-constraint>

</security-constraint>

<security-role>
<role-name>ValidUser</role-name>
</security-role>

<security-role>
<role-name>USER</role-name>
</security-role>

<security-role>
<role-name>MANUFACTURER</role-name>
</security-role>

<security-role>
<role-name>DEVELOPER</role-name>
</security-role>

<security-role>
<role-name>OVERLAY_ADMIN</role-name>
</security-role>

</web-app>

我只得到这一行:

WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response pilotage.rest.incidents.IncidentEditiqueService.uploadFile(javax.ws.rs.core.HttpHeaders,java.lang.Integer,java.lang.String,java.io.InputStream) at index 3.

有人可以帮助我吗?

最佳答案

我有同样的错误。错误是因为错误的包导入。确保导入正确的包。我认为这里的问题是因为 FormDataParam 和 PathParam 包。这两个的正确包是:

  1. PathParam - javax.ws.rs.PathParam;
  2. FormDataParam - org.glassfish.jersey.media.multipart.FormDataParam;

关于java - 上传文件时未找到 public javax.ws.rs.core.Response 类型参数的注入(inject)源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60054836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com