直接上代码
?
? ? @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();
? ? }
}
?