参考网上文章,用CXF发布上传文件接口,并上传大文件的测试。

框架:spring3.1+cxf2.7.6

1.定义文件类实体

import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlMimeType; /**
* CXF上传和下载文件对象包装类 由于CXF的DataHandler无法获取文件名和文件类型,需要在上传和下载时附带文件名
*
*/
public class CxfFileWrapper {
// 文件名
private String fileName = null;
// 文件扩展名
private String fileExtension = null;
// 文件二进制数据
private DataHandler file = null; public final String getFileName() {
return fileName;
} public final void setFileName(String fileName) {
this.fileName = fileName;
} public final String getFileExtension() {
return fileExtension;
} public final void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
} // 注解该字段为二进制流
@XmlMimeType("application/octet-stream")
public final DataHandler getFile() {
return file;
} public final void setFile(DataHandler file) {
this.file = file;
}
}

2.定义服务接口

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService; import net.bwda.domain.upload.CxfFileWrapper; @WebService(name = "FileWS", serviceName = "FileWS")
public interface FileWS {
/**
* 文件上传
* @param file 文件上传包装类
* @return 上传成功返回true,上传失败返回false。
*/
@WebMethod
boolean upload(@WebParam(name = "file") CxfFileWrapper file); /**
* 文件下载
* @return 文件
*/
@WebMethod
CxfFileWrapper download();
} import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource; import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import net.bwda.domain.upload.CxfFileWrapper;
import net.bwda.service.upload.FileWS; @Service
@Component("fileWS")
public class FileWSImpl implements FileWS { public boolean upload(CxfFileWrapper file){
boolean result = true;
OutputStream os = null;
InputStream is = null;
BufferedOutputStream bos = null;
try {
is = file.getFile().getInputStream();
File dest = new File("d:\\upload\\" + file.getFileName());
os = new FileOutputStream(dest);
bos = new BufferedOutputStream(os);
byte[] buffer = new byte[1024*1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
if(bos != null){
try{
bos.close();
}catch(Exception e){
}
}
if(os != null){
try{
os.close();
}catch(Exception e){
}
}
if(is != null){
try{
is.close();
}catch(Exception e){
}
}
}
return result;
} public CxfFileWrapper download() {
//下载文件的路径
String filePath = "D:\\test.xlsx";
CxfFileWrapper fileWrapper = new CxfFileWrapper();
fileWrapper.setFileName("test.xlsx");
fileWrapper.setFileExtension("xlsx");
DataSource source = new FileDataSource(new File(filePath));
fileWrapper.setFile(new DataHandler(source));
return fileWrapper;
} }

3.定义CXF配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
xmlns:cxf="http://cxf.apache.org/core"> <import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <cxf:bus>
<cxf:properties>
<!-- 定义上传文件的最大长度,限制为256M -->
<entry key="org.apache.cxf.stax.maxTextLength" value="268435456" />
</cxf:properties>
</cxf:bus> <jaxws:endpoint id="fileWSEndpoint" implementor="#fileWS"
address="/file">
<jaxws:properties>
<!-- 开启MTOM -->
<entry key="mtom_enabled" value="true"></entry>
</jaxws:properties>
</jaxws:endpoint> </beans>

更多CXF配置,可参见:https://cxf.apache.org/docs/security.html

4.生成客户端代码

d:\apache-cxf-2.7.6\bin\wsdl2java.bat -frontend jaxws21 -p com..test.webservice.file -client http://localhost:9080/filemark/webservice/file?wsdl

5.根据自动上传的代码上传文件测试

/**
* Please modify this class to meet your needs
* This class is not complete
*/ import java.io.File;
import java.net.URL; import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName; /**
* This class was generated by Apache CXF 2.7.6
* 2016-11-10T18:37:44.270+08:00
* Generated source version: 2.7.6
*
*/
public final class FileWS_FileWSImplPort_Client { private static final QName SERVICE_NAME = new QName("http://upload.serviceimpl.bwda.net/", "FileWSImplService"); private FileWS_FileWSImplPort_Client() {
} public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = FileWSImplService.WSDL_LOCATION;
FileWSImplService ss = new FileWSImplService(wsdlURL, SERVICE_NAME);
FileWS port = ss.getFileWSImplPort(); {
System.out.println("Invoking upload...");
net.bwda.Test.webservice.CxfFileWrapper _upload_file = new CxfFileWrapper();
_upload_file.setFileName("test.csv");
_upload_file.setFileExtension("csv");
DataSource source = new FileDataSource(new File("d:\\test.csv"));
_upload_file.setFile(new DataHandler(source));
boolean _upload__return = port.upload(_upload_file);
System.out.println("upload.result=" + _upload__return);
} System.exit(0);
} }

CXF:通过WebService上传文件,包括大文件的处理的更多相关文章

  1. 使用SAS和JavaScript前端上传Azure Bolb大文件

    问题描述: Azure Storage Rest API提供了对于大文件分块上传方法,分别使用Put Block和Put Block List实现相关功能 参考链接: Uploading Large ...

  2. 如何免费上传4G以上大文件至百度云网盘

    百度云网盘的容量高达2048G,因而如今使用百度云网盘的用户也越来越多, 但是百度云中如果要上传超过4G的大文件,必须要升级VIP才行,但这需要收费.那么,超过4G以上的大文件我们该怎样上传到百度云呢 ...

  3. ASPCMS不能上传2M以上大文件修改!

    \admin_aspcms\editor\upload.asp修改80行左右maxattachsize=xxxxxxxx'最大上传大小,默认是2M前提是IIS上传大小已修 其他有关IIS方面的修改: ...

  4. git上传超过100m大文件

    1.git出错如下错误时 执行如下可解决错误: git rm --cache '大文件路径' git commit --amend -CHEAD git push 2.当必须上传大文件时.需借助git ...

  5. 精讲RestTemplate第6篇-文件上传下载与大文件流式下载

    本文是精讲RestTemplate第6篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

  6. 20160113006 asp.net实现ftp上传代码(解决大文件上传问题)

    using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using S ...

  7. 居于H5的多文件、大文件、多线程上传解决方案

    文件上传在web应用中是比较常见的功能,前段时间做了一个多文件.大文件.多线程文件上传的功能,使用效果还不错,总结分享下. 一. 功能性需求与非功能性需求 要求操作便利,一次选择多个文件进行上传: 支 ...

  8. 前端上传视频、图片、文件等大文件 组件Plupload使用指南

    demo:https://blog.csdn.net/qq_30100043/article/details/78491993 Plupload上传插件中文帮助文档网址:http://www.phpi ...

  9. Delphi调用JAVA的WebService上传XML文件(XE10.2+WIN764)

    相关资料:1.http://blog.csdn.net/luojianfeng/article/details/512198902.http://blog.csdn.net/avsuper/artic ...

随机推荐

  1. 轻松自动化---selenium-webdriver(python) (十一)

    本节重点: 控制滚动条到底部 有时候我们需要控制页面滚动条上的滚动条,但滚动条并非页面上的元素,这个时候就需要借助js是来进行操作.一般用到操作滚动条的会两个场景: 注册时的法律条文需要阅读,判断用户 ...

  2. Elasticsearch聚合 之 DateRange日期范围聚合

    相比于range聚合,date range就是范围可以由时间来指定. 例如: { "aggs":{ "range":{ "date_range&quo ...

  3. JS魔法堂:初探传说中的setImmediate函数

    一.前言   由于JavaScript程序为单线程,因此在执行长时间的操作时(如循环和递归操作)到导致UI线程长期被阻塞,无法响应用户操作请求(如点击按钮等),让用户体验大打折扣.于是想到将一个长时间 ...

  4. IOS开发技巧快速生成二维码

    随着移动互联网的发展,二维码应用非常普遍,各大商场,饭店,水果店 基本都有二维码的身影,那么ios中怎么生成二维码呢? 下面的的程序演示了快速生成二维码的方法: 在ios里面要生成二维码,需要借助一个 ...

  5. Chrome开发者工具详解(3)-Timeline面板

    Chrome开发者工具详解(3)-Timeline面板 注: 这一篇主要讲解面板Timeline,参考了Google的相关文档,主要用于公司内部技术分享.. Timeline面板 Timeline面板 ...

  6. HoverTree开发日志之验证码

    HoverTree是一个ASP.NET的开源CMS,目前包含文章系统,图库和留言板功能.代码完全开放,文章内容页生成了静态的HTM页面,留言板提供留言审核功能,文章可以发布HTML源代码,图片上传同时 ...

  7. iOS阶段学习第一天笔记(Mac终端的操作)

    前言部分 原本从事的是.NET开发,一直在要不要转iOS 中犹豫徘徊,经过复杂的内心挣扎终于鼓起勇气辞职脱产学习iOS;希望通过四个月的 学习后能够拿到理想的薪资.以下是学习过程中的学习笔记,为了方便 ...

  8. 如何设计一个 App 的注册登录流程?

    移 动设备发力之前的登录方式很简单:用户名/邮箱+密码+确认密码,所有的用户登录注册都是围绕着邮箱来做.随着移动设备和社交网络的普及,邮箱不再是唯 一,渐渐的出现了微博,QQ,微信等第三方登录方式,手 ...

  9. 基于.Net Framework 4.0 Web API开发(2):ASP.NET Web APIs 参数传递方式详解

    概述:  ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.调用API过程中参数的传递是必须的,本节就来谈谈 ...

  10. 不可或缺 Windows Native (13) - C++: 标准输入, 标准输出, 字符串内存流

    [源码下载] 不可或缺 Windows Native (13) - C++: 标准输入, 标准输出, 字符串内存流 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 标准输 ...