黑马程序员技术交流社区
标题:
关于TCP上传文件的问题 ,求解决
[打印本页]
作者:
stamSuper
时间:
2014-5-8 09:49
标题:
关于TCP上传文件的问题 ,求解决
客户端的上传代码
public static void copyFileClient(){
BufferedInputStream bufin = null;
Socket socket = null;
try {
socket = new Socket("192.168.1.101",2006);
String fileName = "aa.png";
bufin = new BufferedInputStream(new FileInputStream("E:\\test\\"+fileName));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
//先把文件名称传到服务器 ,好确定上传后的文件名称
out.write(fileName.getBytes());
//out.write(System.getProperty("line.separator").getBytes());
out.flush();
复制代码
我tcpServer端的代码 ,当然 我这都是在多线程中运行的,这个方法在线程的实现方法run方法中调用的
public void copyFileServer(){
BufferedOutputStream socOut = null;
BufferedInputStream socIn = null;
BufferedOutputStream fileOut = null;
try {
String ip =socket.getInetAddress().getHostAddress();
socIn =new BufferedInputStream(socket.getInputStream());
socOut = new BufferedOutputStream(socket.getOutputStream());
String path = "E:"+File.separator+"test"+File.separator+"temp"+File.separator;
///通过 流 和默认地址,得到要存放的文件
File file = getSaveFile(socIn,path);
System.out.println(ip+"-----------"+file);
fileOut = new BufferedOutputStream(new FileOutputStream(file));
byte [] buf =new byte[1024];
int len = -1;
while(( len = socIn.read(buf))!=-1){
fileOut.write(buf,0,len);
fileOut.flush();
}
socket.shutdownInput();
System.out.println("------------服务器端已经把数据全部写入到指定文件中"+file+"---------");
socOut.write("已经上传完毕".getBytes());
socOut.flush();
fileOut.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
public static File getSaveFile(InputStream in,String path) throws IOException{
int count =1;
复制代码
运行结果我也贴出来吧
如果我在客户端让线程停止一会儿的,服务器端打印的结果如下
192.168.1.101-----------E:\test\temp\aa(2).png
------------服务器端已经把数据全部写入到指定文件中E:\test\temp\aa(2).png---------
复制代码
如果我在客户端不让线程停止,服务器端打印的结果如下:
192.168.1.101-----------E:\test\temp\aa(1).png�PNG
\032
。。。。后面是一些乱码数据,估计粘贴板不能复制这些数据,反正是粘贴不上来
复制代码
123.jpg
(98.18 KB, 下载次数: 77)
下载附件
2014-5-8 09:48 上传
TCPServer 上传报错
作者:
stamSuper
时间:
2014-5-8 09:54
奇怪 ,上传的代码怎么只有 一部分了,
作者:
stamSuper
时间:
2014-5-8 10:05
我在贴一次代码tcpclient端代码
public static void copyFileClient(){
BufferedInputStream bufin = null;
Socket socket = null;
try {
socket = new Socket("192.168.1.101",2006);
String fileName = "aa.png";
bufin = new BufferedInputStream(new FileInputStream("E:\\test\\"+fileName));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
//先把文件名称传到服务器 ,好确定上传后的文件名称
out.write(fileName.getBytes());
//out.write(System.getProperty("line.separator").getBytes());
out.flush();
/*关键就这这一步,如果我不让当前线程停止一会儿, 后台获取 我这个文件名称的时候 ,会把文件中的数据也获取到 ,我贴的图片可以看到结果 */
/*Thread.sleep(100);*/
byte[] buf = new byte[1024];
int len = -1;
while((len = bufin.read(buf))!= -1){
out.write(buf,0,len);
out.flush();
}
System.out.println("---客户端已经把数据读取完毕--");
socket.shutdownOutput();/// 服务器端一直在等待 读取到客户端传输的结尾位置,而这个方法可以实现
///读取服务器反馈的 信息
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
StringBuilder sb = new StringBuilder();
int l = -1;
System.out.println("*******客户端等待读取服务器端的数据*********");
while((l = in.read(buf))!=-1){
sb.append(new String(buf,0,l));
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(bufin != null)
bufin.close();
if(socket !=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
复制代码
tcpServer端代码
public class DoServer implements Runnable{
复制代码
作者:
stamSuper
时间:
2014-5-8 10:06
tcpServer端代码
public class DoServer implements Runnable{
private Socket socket ;
DoServer(Socket socket ){
this.socket = socket;
}
@Override
public void run() {
copyFileServer();
}
public void copyFileServer(){
BufferedOutputStream socOut = null;
BufferedInputStream socIn = null;
BufferedOutputStream fileOut = null;
try {
String ip =socket.getInetAddress().getHostAddress();
socIn =new BufferedInputStream(socket.getInputStream());
socOut = new BufferedOutputStream(socket.getOutputStream());
String path = "E:"+File.separator+"test"+File.separator+"temp"+File.separator;
///通过 流 和默认地址,得到要存放的文件
File file = getSaveFile(socIn,path);
System.out.println(ip+"-----------"+file);
fileOut = new BufferedOutputStream(new FileOutputStream(file));
byte [] buf =new byte[1024];
int len = -1;
while(( len = socIn.read(buf))!=-1){
fileOut.write(buf,0,len);
fileOut.flush();
}
socket.shutdownInput();
System.out.println("------------服务器端已经把数据全部写入到指定文件中"+file+"---------");
socOut.write("已经上传完毕".getBytes());
socOut.flush();
fileOut.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 通过 socket获取流对象,然后根据流对象获取 客户端发送的要上传的文件名称
* @param in
* @param path
* @return
* @throws IOException
*/
public static File getSaveFile(InputStream in,String path) throws IOException{
int count =1;
//这里不知道客户端上传的文件 名称是多少字符的,我就定义了 差不多 100个字符的长度
byte []bufName = new byte[200];
/*这里获取 客户端发送的文件名称,可能会把文件内容也获取到,这样的话就不是文件的名称了 ,所以服务器会报错 */
int l = in.read(bufName);
String fileName = new String(bufName,0,l);
// String fileName ="test.png";
String sufName = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
String preName = fileName.substring(0, fileName.lastIndexOf("."));
File file = new File(path+preName+"("+count+")"+"."+sufName);
while(file.exists()){
file = new File(path+preName+"("+count++ +")"+"."+sufName);
}
return file;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2