当前位置 : 主页 > 网页制作 > Nodejs >

webservice 文件上传下载之CXF restful

来源:互联网 收集:自由互联 发布时间:2021-06-24
wsimport -extension -keep -encoding UTF-8 -d dist-s sourceswsdl\InspCertMgmt.xml 不多说,直接上代码 本身没有什么难度 ,html5中可以获取到文件名字和文件长度 fastdfs 上传文件需要知道文件大小 不管是

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>

 

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

网友评论