本帖最后由 youcyou 于 2014-5-28 11:12 编辑
代码如下,显示上传成功,文件夹唉创建正常 但是文件夹内无文件- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- public class PicClient {
- public static void main (String args[]) throws IOException
- {
- Socket s = new Socket("192.168.1.108",10005);
-
- FileInputStream fis = new FileInputStream("d:\\1.jpg");
-
- InputStream in = s.getInputStream();
-
- OutputStream out = s.getOutputStream();
-
- byte by[] = new byte[1024];
- int len=0;
- while((len=fis.read())!=-1)
- {
- out.write(by,0,len);
- }
- s.shutdownOutput();
-
- byte b[]=new byte[1024];
- int num = in.read(b);
- String str = new String(b,0,num);
- System.out.println(str);
-
- fis.close();
- s.close();
-
-
- }
- }
复制代码- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class PicServer {
- public static void main(String args[]) throws IOException
- {
- System.out.println("上传服务器开启。。。");
- ServerSocket ss = new ServerSocket(10005);
- while(true)
- {
- Socket s= ss.accept();
- new Thread(new UpPic(s)).start();
- }
-
- //ss.close();
- }
- }
复制代码- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- public class UpPic implements Runnable {
-
- private Socket s;
- UpPic(Socket s)
- {
- super();
- this.s=s;
- }
-
-
- public void run() {
- String ip =s.getInetAddress().getHostAddress();
- System.out.println(ip+"...connected");
-
- File file = getFile("e:\\server_pic", ip);
-
- InputStream in ;
- try
- {
- in = s.getInputStream();
- FileOutputStream fos = new FileOutputStream(file);
-
- byte[] by = new byte[1024];
- int len =0;int count=1;
- while((len=in.read(by))!=-1)
- {
- System.out.println(count++);
- fos.write(by,0,len);
- }
-
- OutputStream out = s.getOutputStream();
- out.write("图片上传成功".getBytes());
-
- fos.close();
- s.close();
-
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- private File getFile(String dir, String ip) {
- // TODO 自动生成的方法存根
- File pic_dir = new File(dir);
- if(!pic_dir.exists())
- {
- pic_dir.mkdir();
- }
-
- int count =1;
- File file = new File(ip+"_"+count+".jpg");
- while(file.exists())
- {
-
- file=new File(ip+"_"+count+".jpg");count++;
- }
- return file;
- }
- }
复制代码 |