webservice 文件上传及下载

本文展示了如何使用WebService和RESTful API实现文件上传和下载。在WebService中,通过`uploadFile`和`downloadFile`方法进行操作,利用DataHandler处理文件流。而在RESTful API中,通过`@POST`和`@GET`注解,结合`@FormDataParam`处理上传和下载请求。示例中详细给出了请求和响应的XML和HTTP内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码

 

    @Override

    public UploadFileResponse uploadFile(UploadFileRequest uploadFileRequest) throws BusinessException, TechnicalException {

 

        UploadFileResponse response = new UploadFileResponse();

        File file = new File("D:/text.txt");

        OutputStream out = null;

        try {

            out = new FileOutputStream(file);

            uploadFileRequest.getFileHandler().writeTo(out);

        } catch (IOException e) {

            throw new TechnicalException(e);

        } finally {

            if (out != null) {

                try {

                    out.close();

                } catch (IOException e) {

                    throw new TechnicalException(e);

                }

            }

        }

        return response;

    }

 

    @Override

    public DownloadFileResponse downloadFile(DownloadFileRequest downloadFileRequest) throws BusinessException, TechnicalException {

 

        DownloadFileResponse response = new DownloadFileResponse();

 

        response.setFileHandler(new DataHandler(new FileDataSource(new File("D:\\move.zip"))));

 

        return response;

    }

 

 

上传下载的vo

 

public class UploadFileRequest extends BasicRequest {

    /**

     * 要上传的文件信息

     */

    private FileInfo file;

    /**

     * 要上传的文件内容

     */

    @XmlMimeType("application/octet-stream") 

    private DataHandler fileHandler;

 

    /**

     * @return the file

     */

    public FileInfo getFile() {

        return file;

    }

 

    /**

     * @param file the file to set

     */

    public void setFile(FileInfo file) {

        this.file = file;

    }

 

    /**

     * @return the fileHandler

     */

    public DataHandler getFileHandler() {

        return fileHandler;

    }

 

    /**

     * @param fileHandler the fileHandler to set

     */

    public void setFileHandler(DataHandler fileHandler) {

        this.fileHandler = fileHandler;

    }

 

}

 

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

public class DownloadFileResponse {

    /**

     * 要上传的文件内容

     */

    @XmlMimeType("application/octet-stream") 

    private DataHandler fileHandler;

 

    /**

     * @return the fileHandler

     */

    @XmlTransient 

    public DataHandler getFileHandler() {

        return fileHandler;

    }

 

    /**

     * @param fileHandler the fileHandler to set

     */

    public void setFileHandler(DataHandler fileHandler) {

        this.fileHandler = fileHandler;

    }

 

}

 

http://localhost:7001/com-bizfty-file-svc/FileGateway

测试下载

 

request

 

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:bean="http://file.bizfty.com/bean">

   <soap:Header/>

   <soap:Body>

      <bean:DownloadFileRequest>

       </bean:DownloadFileRequest>

   </soap:Body>

</soap:Envelope>

 

response

 

<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">

   <S:Body>

      <ns2:DownloadFileResponse xmlns:ns2="http://file.bizfty.com/bean">

         <fileHandler>UEsDBBQAAAAAANtxwkgA。。。。。。</fileHandler>

      </ns2:DownloadFileResponse>

   </S:Body>

</S:Envelope>

 

上传测试

 

方式一 base64

request

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:bean="http://file.bizfty.com/bean">

   <soap:Header/>

   <soap:Body>

      <bean:UploadFileRequest>

         <file>

         </file>

         <fileHandler>UEsDBBQAAAAAANtx</fileHandler>

        </bean:UploadFileRequest>

   </soap:Body>

</soap:Envelope>

response

<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">

   <S:Body>

      <ns2:UploadFileResponse xmlns:ns2="http://file.bizfty.com/bean"/>

   </S:Body>

</S:Envelope>

 

方式二 附件

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:bean="http://file.bizfty.com/bean">

   <soap:Header/>

   <soap:Body>

      <bean:UploadFileRequest>

         <file>

         </file>

         <fileHandler>cid:MasterPDFEditor.rar</fileHandler>

  </bean:UploadFileRequest>

   </soap:Body>

   

</soap:Envelope>

response

<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">

   <S:Body>

      <ns2:UploadFileResponse xmlns:ns2="http://file.bizfty.com/bean"/>

   </S:Body>

</S:Envelope>

 

 

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

再来一个Restful 文件上传下载

@Path("file")

public class FileResource {

 

    @POST

    @Consumes(MediaType.MULTIPART_FORM_DATA)

    public FileInfo upload(

            @FormDataParam("fileName") String fileName,

            @FormDataParam("file") InputStream file,

            @FormDataParam("file") FormDataContentDisposition fileDetail) throws FileNotFoundException, IOException {

        

        FileChannel fileChannel = new FileOutputStream("D:\\test.txt").getChannel(); 

        ReadableByteChannel readChannel = Channels.newChannel(file);  

 

        fileChannel.transferFrom(readChannel, 0, 0);

        FileInfo response = new FileInfo();

        return response;

    }

 

    @GET

    @Path("{id}")

    @Produces(MediaType.APPLICATION_OCTET_STREAM)

    public Response download(@PathParam("id") String id) throws FileNotFoundException {

        File download = new File("D:\\move.zip");

        ResponseBuilder response = Response.ok((Object) download);

        response.header("Content-Disposition", "attachment; filename=move.zip");

        return response.build();

    }

}

 



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值