加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice 文件上传下载之CXF restful

发布时间:2020-12-16 22:05:27 所属栏目:安全 来源:网络整理
导读:wsimport -extension -keep -encoding UTF-8 -d dist?-s sources?wsdlInspCertMgmt.xml ? 不多说,直接上代码 ? 本身没有什么难度 ,html5中可以获取到文件名字和文件长度 fastdfs 上传文件需要知道文件大小 不管是jersey 还是 cxf 文件文件上传都用到了 私

wsimport -extension -keep -encoding UTF-8 -d dist?-s sources?wsdlInspCertMgmt.xml

?

不多说,直接上代码

?

本身没有什么难度 ,html5中可以获取到文件名字和文件长度 fastdfs 上传文件需要知道文件大小

不管是jersey 还是 cxf 文件文件上传都用到了 私用对象,这个不利于 移植。

?

-------------------------------------------------------------------------------

@Path("file")

public class FileRSGatewayImpl {

?

? ? private FileService fileService;

? ? private static final String OPEN_ONLINE_FLAG = "1";

?

? ? private static final org.slf4j.Logger logger = LoggerFactory.getLogger(FileRSGatewayImpl.class);

?

? ? @POST

? ? @Produces(MediaType.APPLICATION_JSON)

? ? public FileInfo upload(

? ? ? ? ? ? @Multipart(value = "name") String name,

? ? ? ? ? ? @Multipart(value = "size") long size,

? ? ? ? ? ? @Multipart(value = "file") Attachment file

? ? ) throws BusinessException,TechnicalException {

? ? ? ? String fileName = name;

? ? ? ? long fileSize = size;

? ? ? ? if(fileName==null){

? ? ? ? ? ? fileName = file.getDataHandler().getName();

? ? ? ? }

? ? ? ? String path = null;

? ? ? ? try {

? ? ? ? ? ? path = fileService.upload(file.getDataHandler().getInputStream(),fileSize,fileName);

? ? ? ? } catch (IOException ex) {

? ? ? ? ? ? logger.error("io exception",ex);

? ? ? ? ? ? throw new TechnicalException("error for upload file:",ex);

? ? ? ? }

? ? ? ? FileInfo fileInfo = new FileInfo();

? ? ? ? fileInfo.setName(fileName);

? ? ? ? fileInfo.setSize(fileSize);

? ? ? ? fileInfo.setMimeType(file.getContentType().toString());

? ? ? ? fileInfo.setPath(path);

? ? ? ? return fileInfo;

? ? }

?

? ? @GET

? ? @Path("{path}/{openOnline}/{contentType}/{name}")

? ? @Produces(MediaType.APPLICATION_OCTET_STREAM)

? ? public Response download(

? ? ? ? ? ? @PathParam("path") String path,

? ? ? ? ? ? @PathParam("openOnline") String openOnline,

? ? ? ? ? ? @PathParam("contentType")String type,

? ? ? ? ? ? @PathParam("name") String name

? ? ) throws BusinessException,TechnicalException {

? ? ? ? String contentType =type;

? ? ? ? StringBuilder sb = new StringBuilder();

? ? ? ? if (OPEN_ONLINE_FLAG.equals(openOnline)) {

? ? ? ? ? ? sb.append(MediaType.APPLICATION_OCTET_STREAM);

? ? ? ? ? ? contentType = MediaType.APPLICATION_OCTET_STREAM;

? ? ? ? } else {

? ? ? ? ? ? sb.append("attachment");

? ? ? ? }

? ? ? ? sb.append(";filename=");

? ? ? ? if (name != null) {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? sb.append(new String(name.getBytes(),"ISO8859-1"));

? ? ? ? ? ? } catch (UnsupportedEncodingException ex) {

? ? ? ? ? ? ? ? logger.warn("name charset error",ex);

? ? ? ? ? ? ? ? sb.append("data");

? ? ? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? sb.append("data");

? ? ? ? }

?

? ? ? ? ResponseBuilder response = Response.ok(fileService.download(path));

? ? ? ? response.header("Content-Disposition",sb.toString());

? ? ? ? response.header("Pragma","No-cache");

? ? ? ? response.header("Cache-Control","No-cache");

? ? ? ? response.header("Expires","0");

? ? ? ? response.header("Content-Type",contentType);

? ? ? ? return response.build();

? ? }

?

? ? /**

? ? ?* @param fileService the fileService to set

? ? ?*/

? ? public void setFileService(FileService fileService) {

? ? ? ? this.fileService = fileService;

? ? }

}

?

--------------------------------------------------------------------------

?

? ? <bean id="jacksonJsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>

? ? <jaxrs:server id="FileResouce" address="/rs">

? ? ? ? <jaxrs:serviceBeans>?

? ? ? ? ? ? <ref bean="fileRSGateway" />

? ? ? ? </jaxrs:serviceBeans>

? ? ? ? <jaxrs:providers>

? ? ? ? ? ? <ref bean="jacksonJsonProvider" />

? ? ? ? </jaxrs:providers>

? ? </jaxrs:server>

?

---------------------------------------------------------------------------------------------

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读