两个tomcat之间传文件
项目将来的需要,之前没做过,这是搜到的一些资料,整理一下,抽空实验一下。
1.用WebService传输文件
(http://hi.baidu.com/ant_boy/blog/item/dbfed36393a6a4670d33fae6.html)
public class FileTransferWs { public int uploadFile( byte []bs, String fileName) { FileOutputStream out = null ; try { String newFile = " C:\tmp\ " + fileName; // 上传文件存放路径 out = new FileOutputStream(newFile); try { out.write(bs); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return - 1 ; } finally { if (out != null ) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return 0 ; } } public class FileUpload { public static void main(String []args) throws Exception { FileTransferWsProxy p = new FileTransferWsProxy(); // 生成webservice代理对象 String filePath = " E:\Book\权证基础知识.pdf " ; String fileName = " 权证基础知识.pdf " ; File file = new File(filePath); FileInputStream in = new FileInputStream(file); byte []bs = new byte [in.available()]; in.read(bs); in.close(); System.out.println( " 正在传输文件“ " + fileName + " ” " ); p.uploadFile(bs, fileName); // 调用webservice进行文件上传 System.out.println( " 文件传输完毕 " ); } }连接建立过程如下:1)server端listen某个端口是否有连接请求2)client端向server端发出connect请求3)server端向client端返回accept消息
通讯过程1)创建socket2)打开连接到socket的输入/输出流3)按照一定的协议 对socket进行读/写操作4)关闭socket