黑马程序员技术交流社区
标题:
我写的文件上传代码,但是出现了错误
[打印本页]
作者:
Mr.Z.Lee
时间:
2013-11-6 19:03
标题:
我写的文件上传代码,但是出现了错误
package com.itheima;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
//客户端
class UpLoadText{
public void main(String[] args) throws Exception {
System.out.println("fffffffff");
Socket s = null;
s = new Socket("localhost",8000);
BufferedReader bufr = new BufferedReader(new FileReader("Test9.java"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line=bufr.readLine())!=null){
out.println(line);
}
s.shutdownOutput();//禁用客户端的输出流,相当于给流中加入结束标记-1。
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
//服务端
class Test9{
public static void main(String[] args) throws Exception {
new UpLoadText().main(args);
ServerSocket ss = new ServerSocket(8000);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("来自IP:"+ip);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new FileWriter("Test9_i.java"),true);
String line = null;
while((line=bufIn.readLine())!=null){
if("over".equals(line))
break;
out.println(line);
}
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");//给客户端发送提示信息。
out.close();
s.close();
ss.close();
}
}
复制代码
作者:
The_Wizard
时间:
2013-11-6 20:08
哥们,你这样写,方式上比我的更好一些。参考一下毕向东老师讲解的 多个客户端上传图片 。通过线程来解决。你的想法估计跟这种方式一样,但你代码写的不对。至少你得开启两个线程,而且得先运行服务器。我自己的代码你先参考下。
class TextClient {
public static void main(String[] args) throws Exception {
InetAddress i = InetAddress.getLocalHost();
Socket s = new Socket(i, 10006);
File file = new File("src\\com\\itheima\\Test7.java");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String fileName = file.getName();
out.println(fileName);
String line = null;
while ((line = br.readLine()) != null) {
out.println(line);
}
s.shutdownOutput();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
br.close();
s.close();
}
}
class TextServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10006);
Socket s = ss.accept();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String fileName = bufIn.readLine();
PrintWriter out = new PrintWriter(new FileWriter(fileName), true);
String line = null;
while ((line = bufIn.readLine()) != null) {
out.println(line);
}
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println("上传成功");
out.close();
s.close();
ss.close();
}
}
作者:
~﹏~
时间:
2013-11-6 20:24
将这个:
BufferedReader bufr = new BufferedReader(new FileReader("Test9.java"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line=bufr.readLine())!=null){
out.println(line);
}
复制代码
改为这个:
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos=new BufferedOutputStream(this.os);
byte[] bytes=new byte[1];
while(bis.read(bytes)!=-1)
{
bos.write(bytes);
}
bis.close();
bos.close();
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2