CXF 实现 REST 方式上传文件
/**
*
*/
package com.demo.rest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.activation.DataHandler;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.springframework.stereotype.Service;
/**
* @author michael
*
*/
@Path("/image")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Service
public class ImageServiceResource {
private static final String UPLOAD_FILE_NAME ="filename";
private static final String UPLOAD_FILE_TEMP ="tmp";
@POST
@Path("/upload/{task}")
@Consumes({ "multipart/form-data" })
public boolean attachImage(@PathParam("task") int task, MultipartBody body) {
RestResult result = new RestResult();
try {
addAttachment(body.getRootAttachment(),task);
} catch (IOException e) {
} catch (Exception e) {
}
return true;
}
@POST
@Path("/uploads/{task}")
@Consumes({ "multipart/form-data" })
public RestResult attachImages(@PathParam("task") int task, MultipartBody body) {
RestResult result = new RestResult();
for (Attachment attachment : body.getAllAttachments()) {
try {
addAttachment(attachment,task);
} catch (IOException e) {
} catch (Exception e) {
}
}
return true;
}
public void addAttachment(Attachment attachment, int task) throws Exception {
try {
DataHandler dataHandler = attachment.getDataHandler();
ContentDisposition cd =attachment.getContentDisposition();
String path = "c:/temp/";
String name = cd.getParameter(UPLOAD_FILE_NAME);
if(name==null){
name =UPLOAD_FILE_TEMP+(new Date()).getTime()+"."+"jpg";
}
UploadFileUtils.upload4Stream(name, path, dataHandler.getInputStream());
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
}
}
}
分享到:
相关推荐
SPRING-MVC-MQ-CXF-REST_Demo
<import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
cxf-core-3.0.0.jar,cxf-rt-bindings-soap-3.0.0.jar,cxf-rt-databinding-jaxb-3.0.0.jar,cxf-rt-frontend-jaxws-3.0.0.jar,cxf-rt-frontend-simple-3.0.0.jar,cxf-rt-transports-http-3.0.0.jar,cxf-rt-...
apache-cxf-2.7.7以及cxf客户端所需要的jar包,命令生成webservice客户端
webservice cxf apache-cxf-2.2.6 开发包
赠送jar包:cxf-rt-transports-http-3.0.1.jar; 赠送原API文档:cxf-rt-transports-http-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-transports-http-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-transports-...
cxf的jar包,请随意下载。?乛?乛?
apache-cxf-2.5.2
spring 4.2.0 集成的cxf3.1.18的jar包,cxf-core-3.1.18.jar、cxf-rt-bindings-soap-3.1.18.jar、cxf-rt-databinding-jaxb-3.1.18.jar、cxf-rt-frontend-jaxws-3.1.18.jar、cxf-rt-frontend-simple-3.1.18.jar、cxf-...
cxf-core-3.1.16版本jar包,用于java开发使用
apache-cxf-2.6.1.zip cxf转化wsdl文件,生成javaBean实体类 附上使用文章:http://blog.csdn.net/sinat_34979884/article/details/78776305
cxf-rt-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。
赠送jar包:cxf-core-3.0.1.jar; 赠送原API文档:cxf-core-3.0.1-javadoc.jar; 赠送源代码:cxf-core-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-core-3.0.1.pom; 包含翻译后的API文档:cxf-core-3.0.1-...
目前最新版本的CXF包,可以辅助开发Web Service的服务器端和客户端
cxf-2.7.18
CXF整合spring实现REST接口开发
apache-cxf-2.7.18-src.zip
cxf开发rest风格接口最少jar
apache-cxf-2.7.6.rar webservice