如何使用Web Service传输文件
server对外只开放80端口,并且还需要提供文件上传和下载功能的应用,下面yjbys小编为大家准备了关于如何使用Web Service传输文件的文章,欢迎阅读。
1. 首先是一个封装了服务器端文件路径,客户端文件路径和要传输的字节数组的MyFile类。
package transfer;
public class MyFile {
private String clientFile;
private String serverFile;
private long position;
private byte[] bytes;
public String getClientFile() {
return clientFile;
}
public void setClientFile(String clientFile) {
ntFile = clientFile;
}
public String getServerFile() {
return serverFile;
}
public void setServerFile(String serverFile) {
erFile = serverFile;
}
public long getPosition() {
return position;
}
public void setPosition(long position) {
tion = position;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
s = bytes;
}
}
2. 文件传输的Web Service接口
package transfer;
import ethod;
import ervice;
@WebService
public interface FileTransferService {
@WebMethod
void uploadFile(MyFile myFile) throws FileTransferException;
@WebMethod
MyFile downloadFile(MyFile myFile) throws FileTransferException;
}
3. 文件传输的Web Service接口实现类,主要是一些流的操作
package transfer;
import ;
import InputStream;
import ception;
import tStream;
import utStream;
import ys;
import Utils;
import ils;
public class FileTransferServiceImpl implements FileTransferService {
public void uploadFile(MyFile myFile) throws FileTransferException {
OutputStream os = null;
try {
if (osition() != 0) {
os = OutputStream(new File(erverFile()), true);
} else {
os = OutputStream(new File(erverFile()), false);
}
e(ytes());
} catch(IOException e) {
throw new FileTransferException(essage(), e);
} finally {
eQuietly(os);
}
}
public MyFile downloadFile(MyFile myFile) throws FileTransferException {
InputStream is = null;
try {
is = new FileInputStream(erverFile());
(osition());
byte[] bytes = new byte[1024 * 1024];
int size = (bytes);
if (size > 0) {
byte[] fixedBytes = OfRange(bytes, 0, size);
ytes(fixedBytes);
} else {
ytes(new byte[0]);
}
} catch(IOException e) {
throw new FileTransferException(essage(), e);
} finally {
eQuietly(is);
}
return myFile;
}
}
4. 一个简单的`文件传输异常类
package transfer;
public class FileTransferException extends Exception {
private static final long serialVersionUID = 1L;
public FileTransferException() {
super();
}
public FileTransferException(String message, Throwable cause) {
super(message, cause);
}
public FileTransferException(String message) {
super(message);
}
public FileTransferException(Throwable cause) {
super(cause);
}
}
5. 下面是Server类用来发布web service
package transfer;
import oint;
public class FileTransferServer {
public static void main(String[] args) throws Exception {
ish("http://localhost:9000/ws/jaxws/fileTransferService", new FileTransferServiceImpl());
}
}
6. 最后是Client类,用来发送文件上传和下载请求。
package transfer;
import ;
import InputStream;
import ception;
import tStream;
import utStream;
import ys;
import Utils;
import ils;
import sProxyFactoryBean;
public class FileTransferClient {
private static final String address = "http://localhost:9000/ws/jaxws/fileTransferService";
private static final String clientFile = "/home/fkong/temp/client/";
private static final String serverFile = "/home/fkong/temp/server/";
public static void main(String[] args) throws Exception {
long start = entTimeMillis();
// uploadFile();
// downloadFile();
long stop = entTimeMillis();
tln("Time: " + (stop - start));
}
private static void uploadFile() throws FileTransferException {
InputStream is = null;
try {
MyFile myFile = new MyFile();
is = new FileInputStream(clientFile);
byte[] bytes = new byte[1024 * 1024];
while (true) {
int size = (bytes);
if (size <= 0) {
break;
}
byte[] fixedBytes = OfRange(bytes, 0, size);
lientFile(clientFile);
erverFile(serverFile);
ytes(fixedBytes);
uploadFile(myFile);
osition(osition() + th);
}
} catch(IOException e) {
throw new FileTransferException(essage(), e);
} finally {
eQuietly(is);
}
}
private static void uploadFile(MyFile myFile) throws FileTransferException {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
ddress(address);
erviceClass(s);
Object obj = te();
FileTransferService service = (FileTransferService) obj;
adFile(myFile);
}
private static void downloadFile() throws FileTransferException {
MyFile myFile = new MyFile();
erverFile(serverFile);
long position = 0;
while (true) {
osition(position);
myFile = downloadFile(myFile);
if (ytes()th <= 0) {
break;
}
OutputStream os = null;
try {
if (position != 0) {
os = OutputStream(new File(clientFile), true);
} else {
os = OutputStream(new File(clientFile), false);
}
e(ytes());
} catch(IOException e) {
throw new FileTransferException(essage(), e);
} finally {
eQuietly(os);
}
position += ytes()th;
}
}
private static MyFile downloadFile(MyFile myFile) throws FileTransferException {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
ddress(address);
erviceClass(s);
Object obj = te();
FileTransferService service = (FileTransferService) obj;
return loadFile(myFile);
}
}
首先需要准备一个大一点的文件,然后修改代码中的clientFile和serverFile路径,然后分别打开uploadFile和downloadFile注释,运行程序,检查目标文件查看结果。
这个程序还是比较简单的,但基本生完成了文件上传下载功能,如果需要,也可以对这个程序再做点修改使其支持断点续传。
-
j2ee入门知识
J2EE初学者要理解的几个问题,在许多企业级应用中,例如数据库连接、邮件服务、事务处理等都是一些通用企业需求模块,这些模块如果每次再开发中都由开发人员来完成的话,将会造成开发周期长和代码可靠性差等问题。一、J2EE提出的背景1、企业级应用框架的需求在许多企...
-
java命令行运行jar包详解2016
jar包是什么呢?jar是javaarchivefile的简写。从名字就可以知道,它的应用与Java息息相关。下面就详细介绍如何自己生成jar包,这样我们管理我们自己的代码(尤其是一些比较重要而且不会或者很少有修改的代码)。下面一起来学习jar命令的一些常用方法,仅供参考!安装...
-
J2EE开发使用手册目录
第一部分企业系统第1章J2EE企业开发1.1企业系统概述1.2J2EE企业系统方案模型1.3J2EE体系结构1.4J2EE企业数据支持1.5J2EE企业通信支持1.6J2EE公共企业服务1.7J2EE企业Web支持1.8J2EE企业应用程序支持1.9小结第二部分企业数据支持第2章企业数据2.1企业数据概述2...
-
2016最新J2EE测试题题库
一、JAVA语言1、面向对象的三个基本特征2、方法重载和方法重写的概念和区别3、接口和内部类、抽象类的特征4、文件读写的基本类5、串行化的注意事项以及如何实现串行化6、线程的基本概念、线程的本状态以及状态之间的关系STOP(),SUSPENG()STOP的本意是中止一个...