[API Server]jersey 檔案多檔上傳

1. POM
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.17</version>
    </dependency>  
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet-core</artifactId>
      <version>2.17</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-multipart</artifactId>
      <version>2.17</version>
    </dependency>

2.上傳網頁
<html>
  <head>
    <title<TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <form action="API/file/MultiUpload" method="post" enctype="multipart/form-data" >
      <p>
        Select a file : <input type="file" name="file" />
      </p>
      <p<
        Select a file : <input type="file" name="file" />
      </p>
      <input type="submit" value="Upload It" />
    </form>

  </body>
</html>

3.API Server 設定

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

@ApplicationPath("API")
public class APIService extends ResourceConfig {

  public APIService() {
    packages("test.API");
    //須註冊這個才可以上傳檔案
     register(MultiPartFeature.class);
  }
}

4.上傳API撰寫

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@Path("/file")
public class UploadFileService {
  @POST
  @Path("/MultiUpload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  @Produces(MediaType.TEXT_HTML + "; charset=UTF-8")
  public Response uploadmutliFile(
          FormDataMultiPart formData
  ) throws UnsupportedEncodingException {
    // 取出多值的資料 file
    List parts = formData.getFields("file");    //檔案說明
    FormDataContentDisposition disp = null;
    //檔案串流
    InputStream inputstream = null;
    //檔案名稱
    String fileName = "";
    //檔案目錄
    String folder = "D://";
    //檔案路徑
    String uploadedFileLocation = "";
    for (FormDataBodyPart part : parts) {
      //取得檔案說明
      disp = part.getFormDataContentDisposition();
      //取得檔案串流
      inputstream = part.getValueAs(InputStream.class);
      //檔名及編碼設定
      fileName = new String(disp.getFileName().getBytes("ISO8859-1"), "UTF-8");
      //設定檔案路徑
      uploadedFileLocation = folder + fileName;
      //寫入
      writeToFile(inputstream, uploadedFileLocation);
    }
    return Response.status(200).entity("Success").build();
  }
  // 寫入資料夾內
  private void writeToFile(
          InputStream uploadedInputStream,
          String uploadedFileLocation) {
    OutputStream out;
    try {
      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();
    }
  }
}
參考:http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
有處理兩個問題

[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response的問題
 ->register(MultiPartFeature.class);

中文檔案亂碼
 ->String fileName = new String(fileDetail.getFileName().getBytes("ISO8859-1"), "UTF-8");

若有疑問可在提出:)






留言

這個網誌中的熱門文章

[java]BIG5 字集判斷

[java]半型轉全型

[javascript]當月第一天、當月最後一天