Tcp文件上传老毕的视屏中文件名都是本地产生的比较固定的文件名。在服务器端怎么生成和上传文件名相同的文件呢,本人想到一个解决的办法:
在开是上传文件之前,先获得本地文件名或者地址都行,然后建立一个专门用于传递文件名的一个Socket,并在上传完毕以后结束该连接,这里要十分的小心阻塞式方法,以防程序停留在阻塞方法上,不能继续程序的运行。在发送完文件名以后其余的事情就和老毕视频里讲的基本一样,区别是在服务器端存文件是用的文件名是通过刚才发送的文件名。
下面是具体实现:
-----------------------------------------------------------------------------------------------------------------------------------
package FileUpload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
class ClientThread
implements Runnable
{
public void run()
{
try
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Socket s = null;
Socket s1 = null;
String destFile = null;
BufferedInputStream bis = null;
System.out.println("请输入一个存在的文件路径:");
label283: while (!((destFile = bf.readLine()).equals("over")))
try
{
bis = new BufferedInputStream(new FileInputStream(destFile));
if (new File(destFile).isFile())
{
s = new Socket("127.0.0.1", 10001);
s1 = new Socket("127.0.0.1", 10002);
PrintWriter pw = new PrintWriter(s1.getOutputStream(), true);
pw.println(destFile);
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b)) != -1)
{
bos.write(b, 0, len);
}
System.out.println("测试标记:");
s.shutdownOutput();
BufferedReader bfInform = new BufferedReader(new InputStreamReader(s.getInputStream()));
String inform = bfInform.readLine();
System.out.println(inform);
System.out.println("请输入文件地址:"); break label283:
}
if (!(new File(destFile).isFile()))
System.out.println("您输入的不是文件路径,请重新输入:");
}
catch (FileNotFoundException e)
{
System.out.println("您输入的不是文件路径,请重新输入:");
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
package FileUpload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
class ServerThread
implements Runnable
{
public void run()
{
try
{
String fileName = null;
ServerSocket ss = new ServerSocket(10001);
ServerSocket ss1 = new ServerSocket(10002);
Socket s = null;
Socket s1 = null;
System.out.println("服务器启动:");
System.out.println("准备接收文件:");
String save = "d:" + File.separator + "ClientUploadFile";
s = ss.accept();
s.setKeepAlive(true);
s1 = ss1.accept();
s1.setKeepAlive(true);
BufferedReader bf1 = new BufferedReader(new InputStreamReader(s1.getInputStream()));
fileName = new File(bf1.readLine()).getName();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "::" + "连接");
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(save + File.separator + fileName));
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println("文件上传成功!");
System.out.println("接收文件成功!");
}
catch (SocketException e)
{
e.printStackTrace(); return;
}
catch (FileNotFoundException e) {
e.printStackTrace(); return;
}
catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------
以上是两个线程类,然后创建各自运行的线程就可以测试了,注意要在两个不同的main方法中创建服务端和客户端线程。 |
|