import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TCPUploadClien1 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Socket s = new Socket("127.0.0.1",10006);
OutputStream os = s.getOutputStream();
FileInputStream fis = new FileInputStream("D:\\1.jpg");
int length = 0;
byte [] buffer = new byte[1024];
while((length = fis.read(buffer))!=-1){
os.write(buffer, 0, length);
}
s.shutdownOutput();
InputStream ins= s.getInputStream();
byte [] src = new byte[1024];
int num = 0;
while((num = ins.read(src))!=-1){
System.out.println(new String(src,0,num));
}
s.close();
}
}
==============================================================
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPUploadServer1 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ServerSocket ss = new ServerSocket(10006);
while(true){
Socket s = ss.accept();
new Thread(new picThread(s)).start();
}
}
}
class picThread implements Runnable{
private Socket s;
public picThread(Socket s){
this.s=s;
}
@Override
public void run() {
int count=1;
// TODO Auto-generated method stub
String ip = s.getInetAddress().getHostAddress();
try {
InputStream is = s.getInputStream();
File file = new File("f:\\"+ip+"("+(count)+")"+".jpg");//是这个file
while(file.exists())
file = new File(ip+"("+(count++)+")"+".jpg");//而不是这个file,但是怎么打印aaa
//这个file没有被反问道,怎么会这样呢
//while结束 file就死了 不是,要不这样
System.out.println(file);
FileOutputStream os = new FileOutputStream("f:\\"+file);//你看
byte [] buffer = new byte[1024];
while(is.read(buffer)!=-1){
os.write(buffer, 0, buffer.length);
}
OutputStream ops = s.getOutputStream();
ops.write("上传成功".getBytes());
os.close();
s.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
===============================================
127.0.0.1(1).jpg
127.0.0.1(1).jpg
127.0.0.1(1).jpg
输出结果总是1,我希望结果每次加1,请高手解决 |
|