JAX-RS之上传文件
来源:互联网 发布:mac 触摸板 鼠标 消失 编辑:程序博客网 时间:2024/06/05 12:46
今天学习的是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 代码如下,先列出,再讲解:
这里,用户选择了文件上传后,会URL根据REST的特性,自动map
到uploadFile方法中,然后通过:
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<InputPart> inputParts = uploadForm.get("uploadedFile");
找出所有的上传文件框(可以是多个),然后进行循环工作:
首先是获得每个文件头的HEADER,用这个
MultivaluedMap<String, String> header = inputPart.getHeaders();
然后在header中取出文件名,这里使用的方法是getFileName,另外写了个方法:
这里,比较麻烦,要获得header,比如header是如下形式的,然后要再提取其中的文件名:
Content-Disposition=[form-data; name="file"; filename="filename.extension"]
最后用writeFile写入磁盘.真麻烦呀,还是用spring mvc好.
2
MultipartForm 的例子, MultipartForm 中,将上传的文件中的属性配置到
POJO中,例子为:FileUploadForm 类,这个POJO类对应上传的文件类.
处理部分就简单多了,可以这样:
即可.
但总的感觉,REST有点蛋疼,上传个文件,用SPIRNG MVC或者其他方法都可以了,还用
REST这个方法?
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 inputstream
- InputStream inputStream = inputPart.getBody(InputStream.class,null);
- byte [] bytes = IOUtils.toByteArray(inputStream);
- //constructs upload file path
- fileName = 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 somewhere
- private 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();
- }
- }
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(); }}
这里,用户选择了文件上传后,会URL根据REST的特性,自动map
到uploadFile方法中,然后通过:
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<InputPart> inputParts = uploadForm.get("uploadedFile");
找出所有的上传文件框(可以是多个),然后进行循环工作:
首先是获得每个文件头的HEADER,用这个
MultivaluedMap<String, String> header = inputPart.getHeaders();
然后在header中取出文件名,这里使用的方法是getFileName,另外写了个方法:
- 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";
- }
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";}
这里,比较麻烦,要获得header,比如header是如下形式的,然后要再提取其中的文件名:
Content-Disposition=[form-data; name="file"; filename="filename.extension"]
最后用writeFile写入磁盘.真麻烦呀,还是用spring mvc好.
2
MultipartForm 的例子, MultipartForm 中,将上传的文件中的属性配置到
POJO中,例子为:FileUploadForm 类,这个POJO类对应上传的文件类.
- 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;
- }
- }
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();
- }
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(); }
即可.
但总的感觉,REST有点蛋疼,上传个文件,用SPIRNG MVC或者其他方法都可以了,还用
REST这个方法?
- JAX-RS之上传文件
- JAX-RS之Jersey入门
- JAX-RS post 上传文件服务 —— 415 - Unsupported Media Type
- 关于使用RESTful api上传文件,基于jax rs接口,不是实现
- 巧用jax-rs之jersey实现不确定数量、多类型文件批量一次上传接口
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS之@matrixParam和@PathParam
- JAX-RS之@QueryParam和@DefaultValue
- JAX-RS之@formparam和@HeaderParam
- JAX-RS之与XML打交道
- JAX-RS之下载文件
- Android学习-计算器1
- 进程地址空间地址转换为物理地址
- 免费的Bug管理系统----BugFree的安装与使用
- java ResultSet获得总行数、总列数的用法详解
- JAX-RS之上传文件
- Davinci内核编译常用命令
- 字符串函数源代码分析与比较
- 【转】用JS实现弹出模式对话框,实现页面传值
- JAX-RS之与XML打交道
- JAX-RS之jackson去处理json
- JAX-RS之resteasy跟spring整合
- 找出文件中每小时最大利用率值
- 合并排序