webservice 文件上传下载之CXF restful

wsimport -extension -keep -encoding UTF-8 -d dist -s sources wsdl\InspCertMgmt.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>

 

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值