Java客户端给服务器发送文件
2015-09-15 17:59
489 查看
public static void sendMsg(String url,int port) throws Exception{
int size=0,length=0;
Socket s = null;
OutputStream os = null;
InputStream is = null;
FileInputStream fis = null;
try {
s = new Socket("localhost",port);
os =s.getOutputStream();
is = s.getInputStream();
File sendFile = new File(url);
fis = new FileInputStream(sendFile);
byte[] buffer = new byte[1024*5];
int i=0;
while((size=is.read(buffer))!=-1){
System.out.println(size+" ");
os.write(buffer,0,size);
os.flush();
System.out.println("第"+ ++i+"次输出");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
is.close();
fis.close();
os.close();
s.close();
}
期中
String url = "//home//test//my//cache//1.odc";
port::对应端口
int size=0,length=0;
Socket s = null;
OutputStream os = null;
InputStream is = null;
FileInputStream fis = null;
try {
s = new Socket("localhost",port);
os =s.getOutputStream();
is = s.getInputStream();
File sendFile = new File(url);
fis = new FileInputStream(sendFile);
byte[] buffer = new byte[1024*5];
int i=0;
while((size=is.read(buffer))!=-1){
System.out.println(size+" ");
os.write(buffer,0,size);
os.flush();
System.out.println("第"+ ++i+"次输出");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
is.close();
fis.close();
os.close();
s.close();
}
期中
String url = "//home//test//my//cache//1.odc";
port::对应端口
相关文章推荐
- win7,win8,win10下配置 Java 环境变量(系统变量)
- SpringMVC 找不到注解实例
- Java学习笔记——泛型
- Java学习笔记(五)数组
- 更改eclipse的Package Explorer的字体
- Java的图片处理工具类【转】
- Java客户端与服务端通信转换必备工具
- Javaweb之JSP九大内置对象
- 【java】查找树按序转双向链表
- java命名规则
- eclipse mar 创建rest风格webservice ---->针对cxf(未完善)
- Solr II——通过SolrJ与Solr服务器进行数据交互
- Java语言中使用OpenMP
- Java Servlet 技术简介
- Java进阶之路 异常
- Java语言中使用OpenMP
- Java multithread
- 关于java普通类怎样获取当前的WebRoot目录。
- Java8 Lambda表达式之比较器
- Struts-config.xml配置文件讲解