CXF webService 实现文件上传与下载
2017-07-18 14:54
309 查看
实际上cxf webservice 的文件上传与下载的操作与普通的file操作思路是一样的。
只不过通过了webservice接口,下面说下细节和代码实现。
如果你不清楚cxf 的 webservice 部署,那么请参照我前面的两篇文章让webservice服务跑起来。
/**
* 全路径:com.webService.Datefile
* 功能描述:封装一个服务器端文件路径,客户端文件路径和要传输的字节数组
*/
public class Datefile {
}
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
/**
* 上传文件
* @param Datefile myFile
* @return String result 正常返回1,错误返回0
*/
public String uploadFile(Datefile myFile) {
OutputStream os = null;
String result = “0” ;
try {
if (myFile.getPosition() != 0) {
//根据指定的文件获取一个新的文件输入流:openInputStream(File file),返回FileOutputStream
os = FileUtils.openOutputStream(
new File(myFile.getServerFile()), true);
} else {
os = FileUtils.openOutputStream(
new File(myFile.getServerFile()), false);
}
os.write(myFile.getBytes());
result = “1”;
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(os);
}
return result;
}
也就是普通的文件流的操作,用了开源的comons.io.FileUtils和IOUtils的简化了几步操作。
调用的时候也是把文件转化成流文件操作写入。
反过来就是下载了,不多说,直接上代码
/**
* 下载文件
* @param Datefile myFile
* @return Datefile myFile
*/
package com.webService.test;
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 com.webService.dateclient.DateService;
import com.webService.dateclient.DateService_Service;
import com.webService.dateclient.Datefile;
public class TestfileUpload {
private static final String clientFile = “F:/我的文档/Java/jdk_a.zip”;
private static final String serverFile = “F:/我的文档/Java/jdk_b.zip”;
static DateService_Service wService = new DateService_Service();
static DateService wi = wService.getDateServicePort();
// downloadFile();
long stop = System.currentTimeMillis();
System.out.println(“Time: ” + (stop - start));
}
}
只不过通过了webservice接口,下面说下细节和代码实现。
如果你不清楚cxf 的 webservice 部署,那么请参照我前面的两篇文章让webservice服务跑起来。
1.封装一个文件对象
package com.webService;/**
* 全路径:com.webService.Datefile
* 功能描述:封装一个服务器端文件路径,客户端文件路径和要传输的字节数组
*/
public class Datefile {
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.上传下载的文件流操作方法
可以写到webservice 接口里,也可以用别的方法调用import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
/**
* 上传文件
* @param Datefile myFile
* @return String result 正常返回1,错误返回0
*/
public String uploadFile(Datefile myFile) {
OutputStream os = null;
String result = “0” ;
try {
if (myFile.getPosition() != 0) {
//根据指定的文件获取一个新的文件输入流:openInputStream(File file),返回FileOutputStream
os = FileUtils.openOutputStream(
new File(myFile.getServerFile()), true);
} else {
os = FileUtils.openOutputStream(
new File(myFile.getServerFile()), false);
}
os.write(myFile.getBytes());
result = “1”;
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(os);
}
return result;
}
也就是普通的文件流的操作,用了开源的comons.io.FileUtils和IOUtils的简化了几步操作。
调用的时候也是把文件转化成流文件操作写入。
反过来就是下载了,不多说,直接上代码
/**
* 下载文件
* @param Datefile myFile
* @return Datefile myFile
*/
public Datefile downloadFile(Datefile myFile){ 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) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } return myFile; }
3.客户端的操作
也是加载成文件流写入或读出的操作。package com.webService.test;
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 com.webService.dateclient.DateService;
import com.webService.dateclient.DateService_Service;
import com.webService.dateclient.Datefile;
public class TestfileUpload {
private static final String clientFile = “F:/我的文档/Java/jdk_a.zip”;
private static final String serverFile = “F:/我的文档/Java/jdk_b.zip”;
static DateService_Service wService = new DateService_Service();
static DateService wi = wService.getDateServicePort();
public static void main(String[] args) { long start = System.currentTimeMillis(); uploadFile();
// downloadFile();
long stop = System.currentTimeMillis();
System.out.println(“Time: ” + (stop - start));
}
private static void uploadFile() { InputStream is = null; try { Datefile datefile = new Datefile(); 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); datefile.setClientFile(clientFile); datefile.setServerFile(serverFile); datefile.setBytes(fixedBytes); wi.uploadFile(datefile); datefile.setPosition(datefile.getPosition() + fixedBytes.length); } } catch(IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } } private static void downloadFile() { Datefile datefile = new Datefile(); datefile.setServerFile(serverFile); long position = 0; while (true) { datefile.setPosition(position); datefile = wi.downloadFile(datefile); if (datefile.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(datefile.getBytes()); } catch(IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(os); } position += datefile.getBytes().length; } }
}
总结
就是基本的文件流的操作,非常简单。相关文章推荐
- java web service 上传下载文件
- 用WebClinet实现SharePoint上文档库中文件的上传与下载
- SAAJ访问javawebService实现文件上传
- CXF-webService实现文件上传下载
- Java web 中实现简单的文件上传与下载——学习笔记
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名
- JavaWeb中struts2实现文件上传下载功能实例解析
- Java - 如何在基于Java的Web项目中实现文件上传和下载?
- 利用Web/WCF Service上传下载文件[网上资源]
- SpringMVC整合fastdfs-client-java实现web文件上传下载
- 用WebClinet实现SharePoint上文档库中文件的上传与下载
- 通过FTP服务端来实现匿名用户和基本用户的访问,可以实现文件上传和下载。通过web网站来浏览内容
- SpringMVC整合fastdfs-client-java实现web文件上传下载
- Web开发之JspSmartUpload实现文件上传下载
- java resteasy restful webservice教程(六):实现文件下载功能实例文章来源:爱上123 原文地址:http://www.ishang123.com/jishubowen/
- JavaWeb之实现文件上传与下载
- VB.NET客户端调用Axis的Java Web Service实现文件上传
- SpringMVC整合fastdfs-client-java实现web文件上传下载
- java web 一行代码实现文件上传下载
- java web 实现 文件上传及下载