您的位置:首页 > 编程语言 > Java开发

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::对应端口
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: