Rest上传文件(利用jersey)

Rest 提交文件

1. 前提是依赖和web.xml中jersey的servlet都配置好了

2. maven依赖库的冲突


        com.sun.jersey
        jersey-servlet
        1.19
    
    
        com.sun.jersey
        jersey-client
        1.19
    
    
        com.sun.jersey.contribs
        jersey-multipart
        1.7
    

加上上述依赖的话会报错,要去掉。

3. 上传和下载的代码的一个例子

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.MimetypesFileTypeMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
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 javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
@Path("/files")
public class FileResource {
private static final String filepath = "E:/circulation-checking-rest/src/resources/download/test1.uml";
private static final String serverLocation = "E:/circulation-checking-rest/src/resources/upload/";
@GET
@Path("download")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile() {

    File file = new File(filepath);
    if (file.isFile() && file.exists()) {
        String mt = new MimetypesFileTypeMap().getContentType(file);
        String fileName = file.getName();

        return Response
                .ok(file, mt)
                .header("Content-disposition",
                        "attachment;filename=" + fileName)
                .header("ragma", "No-cache")
                .header("Cache-Control", "no-cache").build();

    } else {
        return Response.status(Response.Status.NOT_FOUND)
                .entity("下载失败,未找到该文件").build();
    }
}


 @POST
 @Path("upload")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 @Produces("application/json")
 public Response uploadFile(
         @FormDataParam("file") InputStream fileInputStream,
         @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) 
             throws IOException {

     String fileName = contentDispositionHeader.getFileName();
     String t=contentDispositionHeader.getName();

     System.out.println(fileName+" "+t);

     File file = new File(serverLocation + "a.uml"); 
     File parent = file.getParentFile(); 
     //判断目录是否存在,不在创建 
     if(parent!=null&&!parent.exists()){ 
         parent.mkdirs(); 
     } 
     file.createNewFile(); 

     OutputStream outpuStream = new FileOutputStream(file);
     int read = 0;
     byte[] bytes = new byte[1024];

     while ((read = fileInputStream.read(bytes)) != -1) {
         outpuStream.write(bytes, 0, read);
     }

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

     fileInputStream.close();

     return Response.status(Response.Status.OK)
             .entity("Upload Success!").build();
 }
}

4. 上传文件和下载文件的JSP页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>




Insert title here


Download

Upload a File

Select a file :

5. 效果

上传和下载页面

点击下载

上传文件,问价名解析错误

    @FormDataParam("file") InputStream fileInputStream,
    @FormDataParam("file") FormDataContentDisposition contentDispositionHeader

     String fileName = contentDispositionHeader.getFileName();
     String t=contentDispositionHeader.getName();
     System.out.println(fileName+" "+t);

FormDataContentDisposition 这个类在解析文件的地址时,出问题了,路径中的斜杠没了。

你可能感兴趣的:(rest)