2017-04-06 96 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> 

我正在使用Jersey版本2.16,即使我使用的是澤西媒體多媒體類型,我仍然會得到「不支持的媒體類型」 rsion 2.25。有版本問題嗎?或者這個pom.xml有什麼問題?

謝謝,將不勝感激任何幫助。

+0

你可以在你的'uploadFile'方法參數中使用'@FormDataParam(「file」)InputStream fileInputStream',並檢查。 –

+0

怎麼樣?不要讓它..而不是FormDataBodyPart我應該使用FormDataParam? – Pepper

+0

它沒有工作,我得到錯誤500,「org.glassfish.jersey.server.model.ModelValidationException:應用程序資源模型的驗證在應用程序初始化過程中失敗。」 – Pepper

回答

0

您還需要註冊jersey-media-multipart附帶的MultiPartFeature。該功能將註冊處理多部分請求所需的提供程序。您目前看到的錯誤是因爲沒有註冊的供應商可以處理FormDataMultiPart。見this post有幾種不同的註冊方法

相關問題