- public class UploadServer{
- public static void main(String[] args) throws Exception{
- ServerSocket ss = new ServerSocket(10001);
- while(true){
- Socket s = ss.accept();
- new Thread(new UploadThread(s)).start();
- }
- }
- }
- class UploadThread implements Runnable{
- private Socket s;
- UploadThread(Socket s){
- this.s = s;
- }
- public void run() {
- String ip = s.getInetAddress().getHostAddress();
- BufferedInputStream input = null;
- OutputStream out = null;
- //此处我想实现一个方法,就是我如果传递给别人我收到数据了,发送给别人我收到信息的的信息
- PrintWriter pw = null;
- try {
- System.out.println(ip+"...connection");
- input = new BufferedInputStream(s.getInputStream());
- int len = 0;
- byte[] by = new byte[1024];
- StringBuilder sb = new StringBuilder();
- while((len = input.read(by))!=-1){
- sb.append(new String(by,0,len));
- }
- String str = sb.toString();
- //截取通过socket传入数据的名字
- int end = str.lastIndexOf(":");
- //得到文件名称
- String s1 = str.substring(end+1,str.length());
- //要传递的数据
- String s2 = str.substring(1,end);
- File file = new File(s1);
- out = new FileOutputStream("c:\\123\\"+file);
- out.write(s2.getBytes());
- pw = new PrintWriter(s.getOutputStream(),true);
- pw.print("上传成功");
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- //关闭流操作
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
复制代码- //客户端
- public class UploadSocket {
- public static void main(String[] args) {
- //用于键盘录入
- BufferedReader bufr = null;
-
- try {
- bufr = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("输入要传入文件的地址");
- //读取地址
- String file= bufr.readLine();
- File f1 = new File(file);
- //判断文件是否存在
- if(!f1.exists()){
- System.out.println("源文件不存在");
- System.out.println("请重新输入");
- file = bufr.readLine();
- f1 = new File(file);
- }
- System.out.println(f1.getName());
- //调用方法
- setSocket(f1);
-
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- //关闭流
- try {
- bufr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //用于socket传输文件
- public static void setSocket(File file) {
- InputStream input = null;
- OutputStream out = null;
- Socket s = null;
- BufferedReader bufIn= null;
- try {
- input = new FileInputStream(file);
- s = new Socket("127.0.0.1", 10001);
- out = s.getOutputStream();
- int len = 0;
- while ((len = input.read()) != -1) {
- out.write(len);
- }
- //最后在输出文件的名字
- out.write((":"+file.getName()).getBytes());
- bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = bufIn.readLine();
- System.out.println(str);
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
- }
复制代码 输入要传入文件的地址C:\IP.txt
IP.txt
java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at com.zhang.UploadSocket.setSocket(UploadSocket.java:73)
at com.zhang.UploadSocket.main(UploadSocket.java:49)
请大神帮忙看看怎么解决这个问题。
|