首先声明我知道有个协议叫ftp,也知道有种编程叫sock编程,但我就是碰到了server对外只开放80端口,并且还需要提供文件上传和下载功能的应用,那好吧,开始干活。
1. 首先是一个封装了服务器端文件路径,客户端文件路径和要传输的字节数组的MyFile类。
[java] view plaincopyprint?
<EMBED id=ZeroClipboardMovie_1 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package com.googlecode.garbagecan.cxfstudy.filetransfer;
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) {
this.clientFile = clientFile;
}
public String getServerFile() {
return serverFile;
}
public void setServerFile(String serverFile) {
this.serverFile = serverFile;
}
public long getPosition() {
return position;
}
public void setPosition(long position) {
this.position = position;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
}
2. 文件传输的Web Service接口
[java] view plaincopyprint?
<EMBED id=ZeroClipboardMovie_2 name=ZeroClipboardMovie_2 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=2&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package com.googlecode.garbagecan.cxfstudy.filetransfer;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface FileTransferService {
@WebMethod
void uploadFile(MyFile myFile) throws FileTransferException;
@WebMethod
MyFile downloadFile(MyFile myFile) throws FileTransferException;
}
3. 文件传输的Web Service接口实现类,主要是一些流的操作
[java] view plaincopyprint?
<EMBED id=ZeroClipboardMovie_3 name=ZeroClipboardMovie_3 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=3&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package com.googlecode.garbagecan.cxfstudy.filetransfer;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class FileTransferServiceImpl implements FileTransferService {
public void uploadFile(MyFile myFile) throws FileTransferException {
OutputStream os = null;
try {
if (myFile.getPosition() != 0) {
os = FileUtils.openOutputStream(new File(myFile.getServerFile()), true);
} else {
os = FileUtils.openOutputStream(new File(myFile.getServerFile()), false);
}
os.write(myFile.getBytes());
} catch(IOException e) {
throw new FileTransferException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(os);
}
}
public MyFile downloadFile(MyFile myFile) throws FileTransferException {
InputStream is = null;
try {
is = new FileInputStream(myFile.getServerFile());
is.skip(myFile.getPosition());
byte[] bytes = new byte[1024 * 1024];
int size = is.read(bytes);
if (size > 0) {
byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size);
myFile.setBytes(fixedBytes);
} else {
myFile.setBytes(new byte[0]);
}
} catch(IOException e) {
throw new FileTransferException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
}
return myFile;
}
}
4. 一个简单的文件传输异常类
[java] view plaincopyprint?
<EMBED id=ZeroClipboardMovie_4 name=ZeroClipboardMovie_4 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=4&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package com.googlecode.garbagecan.cxfstudy.filetransfer;
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
[java] view plaincopyprint?
<EMBED id=ZeroClipboardMovie_5 name=ZeroClipboardMovie_5 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=5&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package com.googlecode.garbagecan.cxfstudy.filetransfer;
import javax.xml.ws.Endpoint;
public class FileTransferServer {
public static void main(String[] args) throws Exception {
Endpoint.publish("http://localhost:9000/ws/jaxws/fileTransferService", new FileTransferServiceImpl());
}
}
6. 最后是Client类,用来发送文件上传和下载请求。
[java] view plaincopyprint?
<EMBED id=ZeroClipboardMovie_6 name=ZeroClipboardMovie_6 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=6&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package com.googlecode.garbagecan.cxfstudy.filetransfer;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class FileTransferClient {
private static final String address = "http://localhost:9000/ws/jaxws/fileTransferService";
private static final String clientFile = "/home/fkong/temp/client/test.zip";
private static final String serverFile = "/home/fkong/temp/server/test.zip";
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
// uploadFile();
// downloadFile();
long stop = System.currentTimeMillis();
System.out.println("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 = is.read(bytes);
if (size <= 0) {
break;
}
byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size);
myFile.setClientFile(clientFile);
myFile.setServerFile(serverFile);
myFile.setBytes(fixedBytes);
uploadFile(myFile);
myFile.setPosition(myFile.getPosition() + fixedBytes.length);
}
} catch(IOException e) {
throw new FileTransferException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
}
}
private static void uploadFile(MyFile myFile) throws FileTransferException {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setAddress(address);
factoryBean.setServiceClass(FileTransferService.class);
Object obj = factoryBean.create();
FileTransferService service = (FileTransferService) obj;
service.uploadFile(myFile);
}
private static void downloadFile() throws FileTransferException {
MyFile myFile = new MyFile();
myFile.setServerFile(serverFile);
long position = 0;
while (true) {
myFile.setPosition(position);
myFile = downloadFile(myFile);
if (myFile.getBytes().length <= 0) {
break;
}
OutputStream os = null;
try {
if (position != 0) {
os = FileUtils.openOutputStream(new File(clientFile), true);
} else {
os = FileUtils.openOutputStream(new File(clientFile), false);
}
os.write(myFile.getBytes());
} catch(IOException e) {
throw new FileTransferException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(os);
}
position += myFile.getBytes().length;
}
}
private static MyFile downloadFile(MyFile myFile) throws FileTransferException {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setAddress(address);
factoryBean.setServiceClass(FileTransferService.class);
Object obj = factoryBean.create();
FileTransferService service = (FileTransferService) obj;
return service.downloadFile(myFile);
}
}
首先需要准备一个大一点的文件,然后修改代码中的clientFile和serverFile路径,然后分别打开uploadFile和downloadFile注释,运行程序,检查目标文件查看结果。
这个程序还是比较简单的,但基本生完成了文件上传下载功能,如果需要,也可以对这个程序再做点修改使其支持断点续传。