当前位置: 首页 > 知识库问答 >
问题:

上载文件时未找到public javax.ws.rs.core.response类型参数的注入源

宗冷勋
2023-03-14

我想从ReactJS上传一个文件到Java后端。但它会在我上传的每个文件上添加以下内容:

------WebKitFormBoundaryu43OXGtsQPsxs0nM
Content-Disposition: form-data; name="file"; filename="lolo.png"
Content-Type: image/png
<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();
<?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>

警告:在索引3处找不到类型为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)的参数的注入源。

有人能帮帮我吗?

共有1个答案

奚高扬
2023-03-14

我也犯了同样的错误。错误是由于错误的包导入。确保导入正确的包。我认为这里的问题是因为FormDataParam和PathParam包。这两种软件的正确包是:

  1. PathParam-javax.ws.rs.PathParam;
  2. FormDataParam-org.glassfish.jersey.media.multipart.FormDataParam;
 类似资料: