2017-04-06 4 views
1

stackoverflow에서 여러 답변을 보았지만 내 pom.xml에 어떤 문제가 있는지 확인할 수 없습니다. 나는 서버에 파일을 업로드해야합니다, 그래서 나는이 코드를 FormDataMultiPart에서 this linkJersey API에서 지원되지 않는 미디어 유형 오류

의 수정 된 코드를 사용하고 있습니다 :

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 org.glassfish.jersey.media.multipart.ContentDisposition; 
import org.glassfish.jersey.media.multipart.FormDataBodyPart; 
import org.glassfish.jersey.media.multipart.FormDataMultiPart; 

@Path("/files") 
public class JerseyFileUpload { 

private static final String SERVER_UPLOAD_LOCATION_FOLDER = "E:/Upload_Files/"; 

@POST 
@Path("/upload") 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response uploadFile(FormDataMultiPart form) { 

    FormDataBodyPart filePart = form.getField("file"); 

    ContentDisposition headerOfFilePart = filePart.getContentDisposition(); 

    InputStream fileInputStream = filePart.getValueAs(InputStream.class); 

    String filePath = SERVER_UPLOAD_LOCATION_FOLDER + headerOfFilePart.getFileName(); 

    // save the file to the server 
    saveFile(fileInputStream, filePath); 

    String output = "File saved to server location using FormDataMultiPart : " + filePath; 

    return Response.status(200).entity(output).build(); 

} 

// save uploaded file to a defined location on the server 
private void saveFile(InputStream uploadedInputStream, String serverLocation) { 

    try { 
     OutputStream outpuStream = new FileOutputStream(new File(
       serverLocation)); 
     int read = 0; 
     byte[] bytes = new byte[1024]; 

     outpuStream = new FileOutputStream(new File(serverLocation)); 
     while ((read = uploadedInputStream.read(bytes)) != -1) { 
      outpuStream.write(bytes, 0, read); 
     } 

     outpuStream.flush(); 
     outpuStream.close(); 

     uploadedInputStream.close(); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 

} 

} 

내 pom.xlm이

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

<modelVersion>4.0.0</modelVersion> 

<groupId>Photographer</groupId> 
<artifactId>Assignment</artifactId> 
<packaging>war</packaging> 
<version>0.0.1-SNAPSHOT</version> 
<name>Assignment</name> 

<build> 
    <finalName>Assignment</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.5.1</version> 
      <inherited>true</inherited> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.glassfish.jersey</groupId> 
      <artifactId>jersey-bom</artifactId> 
      <version>${jersey.version}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 
    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-servlet-core</artifactId> 
     <!-- use the following artifactId if you don't need servlet 2.x compatibility --> 
     <!-- artifactId>jersey-container-servlet</artifactId --> 
    </dependency> 
    <!-- uncomment this to get JSON support--> 
    <dependency> 
     <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-moxy</artifactId> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.18</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart --> 
    <dependency> 
     <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-multipart</artifactId> 
     <version>2.25</version> 
    </dependency>    

    <dependency> 
     <groupId>org.glassfish.jersey.core</groupId> 
     <artifactId>jersey-server</artifactId> 
    </dependency> 

</dependencies> 
<properties> 
    <jersey.version>2.16</jersey.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 
과 같다

저지 버전 2.16을 사용하고 있습니다. 저지 - 미디어 - 멀티 파트를 사용하고 있는데도 여전히 "지원되지 않는 미디어 유형"이 나타납니다. 2.25. 버전으로 인해 문제가 있습니까? 또는이 pom.xml에 어떤 문제가 있습니까?

감사합니다. 도움을 주셔서 감사합니다.

+0

당신의'uploadFile'메소드 인자와 check에서'@FormDataParam ("file") InputStream fileInputStream을 사용할 수 있습니까? –

+0

How ?? FormDataBodyPart 대신 FormDataParam을 사용해야합니까? – Pepper

+0

작동하지 않습니다. 오류 500이 발생합니다. "org.glassfish.jersey.server.model.ModelValidationException : 응용 프로그램 초기화 중에 응용 프로그램 자원 모델의 유효성 검사가 실패했습니다." – Pepper

답변

0

jersey-media-multipart과 함께 제공되는 MultiPartFeature을 등록해야합니다. 이 기능은 멀티 파트 요청을 처리하는 데 필요한 공급자를 등록합니다. 현재보고있는 오류는 FormDataMultiPart을 처리 할 수있는 등록 된 공급자가 없기 때문입니다. 그것들을 등록하는 몇 가지 다른 방법은 this post을 참조하십시오.

관련 문제