荟萃馆

位置:首页 > IT认证 > J2EE

如何使用Web Service传输文件

J2EE2.55W

server对外只开放80端口,并且还需要提供文件上传和下载功能的应用,下面yjbys小编为大家准备了关于如何使用Web Service传输文件的文章,欢迎阅读

如何使用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注释,运行程序,检查目标文件查看结果。

这个程序还是比较简单的,但基本生完成了文件上传下载功能,如果需要,也可以对这个程序再做点修改使其支持断点续传。