JAX-RS之上传文件
今天学习的是jax-rs中的上传文件.
1 首先要包含的是resteasy-multipart-provider.jar这个文件
2) 之后是简单的HTML FORM
<html>
<body>
<h1>JAX-RS Upload Form</h1>
<form action="rest/file/upload" method="post" enctype="multipart/form-data">
Select a file : <input type="file" name="uploadedFile" size="50" />
<input type="submit" value="Upload It" />
</form>
</body>
</html>
3 代码如下,先列出,再讲解:
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.List;import java.util.Map;import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.core.MultivaluedMap;import javax.ws.rs.core.Response;import org.apache.commons.io.IOUtils;import org.jboss.resteasy.plugins.providers.multipart.InputPart;import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; @Path("/file")public class UploadFileService { private final String UPLOADED_FILE_PATH = "d:\"; @POST@Path("/upload")@Consumes("multipart/form-data")public Response uploadFile(MultipartFormDataInput input) { String fileName = ""; Map<String, List<InputPart>> uploadForm = input.getFormDataMap();List<InputPart> inputParts = uploadForm.get("uploadedFile"); for (InputPart inputPart : inputParts) { try { MultivaluedMap<String, String> header = inputPart.getHeaders();fileName = getFileName(header); //convert the uploaded file to inputstreamInputStream inputStream = inputPart.getBody(InputStream.class,null); byte [] bytes = IOUtils.toByteArray(inputStream); //constructs upload file pathfileName = UPLOADED_FILE_PATH + fileName; writeFile(bytes,fileName); System.out.println("Done"); } catch (IOException e) {e.printStackTrace(); } } return Response.status(200) .entity("uploadFile is called, Uploaded file name : " + fileName).build(); } /** * header sample * { * Content-Type=[image/png], * Content-Disposition=[form-data; name="file"; filename="filename.extension"] * } **///get uploaded filename, is there a easy way in RESTEasy?private String getFileName(MultivaluedMap<String, String> header) { String[] contentDisposition = header.getFirst("Content-Disposition").split(";"); for (String filename : contentDisposition) {if ((filename.trim().startsWith("filename"))) { String[] name = filename.split("="); String finalFileName = name[1].trim().replaceAll(""", "");return finalFileName;}}return "unknown";} //save to somewhereprivate void writeFile(byte[] content, String filename) throws IOException { File file = new File(filename); if (!file.exists()) {file.createNewFile();} FileOutputStream fop = new FileOutputStream(file); fop.write(content);fop.flush();fop.close(); }}
private String getFileName(MultivaluedMap<String, String> header) { String[] contentDisposition = header.getFirst("Content-Disposition").split(";"); for (String filename : contentDisposition) {if ((filename.trim().startsWith("filename"))) { String[] name = filename.split("="); String finalFileName = name[1].trim().replaceAll(""", "");return finalFileName;}}return "unknown";}
import javax.ws.rs.FormParam;import org.jboss.resteasy.annotations.providers.multipart.PartType; public class FileUploadForm { public FileUploadForm() {} private byte[] data; public byte[] getData() {return data;} @FormParam("uploadedFile")@PartType("application/octet-stream")public void setData(byte[] data) {this.data = data;} }
Path("/file")public class UploadFileService { @POST@Path("/upload")@Consumes("multipart/form-data")public Response uploadFile(@MultipartForm FileUploadForm form) { String fileName = "d:\\anything"; try {writeFile(form.getData(), fileName);} catch (IOException e) { e.printStackTrace();} System.out.println("Done"); return Response.status(200) .entity("uploadFile is called, Uploaded file name : " + fileName).build(); }