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

webservice 文件上传及下载

发布时间:2020-12-16 22:06:37 所属栏目:安全 来源:网络整理
导读:直接上代码 ? ? ? @Override ? ? public UploadFileResponse uploadFile(UploadFileRequest uploadFileRequest) throws BusinessException,TechnicalException { ? ? ? ? ? UploadFileResponse response = new UploadFileResponse(); ? ? ? ? File file = ne

直接上代码

?

? ? @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);

? ? ? ? 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();

? ? }

}

?


?

(编辑:李大同)

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

    推荐文章
      热点阅读