[API Server]jersey 檔案上傳

1. POM
   
      org.glassfish.jersey.containers
      jersey-container-servlet
      2.17
   
      
      org.glassfish.jersey.containers
      jersey-container-servlet-core
      2.17
   
   
      org.glassfish.jersey.media
      jersey-media-multipart
      2.17
   

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/upload" method="post" enctype="multipart/form-data" >

      <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("/upload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  @Produces(MediaType.TEXT_HTML + "; charset=UTF-8")
  public Response uploadFile(
          @FormDataParam("file") InputStream uploadedInputStream,
          @FormDataParam("file") FormDataContentDisposition fileDetail) throws UnsupportedEncodingException {
    //UTF-8編碼否則會變亂碼
    String fileName = new String(fileDetail.getFileName().getBytes("ISO8859-1"), "UTF-8");
    //for windows
    String uploadedFileLocation = "d://" + fileName;

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    return Response.status(200).entity(output).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]當月第一天、當月最後一天