當前位置:才華齋>IT認證>J2EE>

如何使用Web Service傳輸檔案

J2EE 閱讀(2.44W)

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註釋,執行程式,檢查目標檔案檢視結果。

這個程式還是比較簡單的,但基本生完成了檔案上傳下載功能,如果需要,也可以對這個程式再做點修改使其支援斷點續傳。