本文实例为大家分享了Java实现文件上传服务器和客户端的具体代码,供大家参考,具体内容如下
文件上传服务器端:
public class UploadServer {
public static void main(String[] args) {
UploadServer server=new UploadServer();
UploadThread command=new UploadThread();
server.start(command);
}
private void start(UploadThread command){
//局部变量
ServerSocket serverSocket = null;
Socket transSocket;
//业务逻辑
try {
serverSocket=new ServerSocket(55555);
while(true){
System.out.println("等待连接……");
transSocket=serverSocket.accept();
int i=0;
i++;
System.out.println("第"+i+"个连接");
//用不用在下载完后关闭线程呢???
command.setSocket(transSocket);
Executors.newFixedThreadPool(5).execute(command);
}
//异常捕获
} catch (IOException e) {
e.printStackTrace();
//关闭资源
} finally{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}//End of try
}//End of connect
@Test
public void testConnect() {
//测试任务:先运行服务器端,然后多次运行客户端,服务器段可以不断创建子线程,则测试成功
//测试准备:构造一个线程,用于模拟下载线程
UploadThread command=new UploadThread();
start(command);
}
@Test
public void testDown() throws IOException {
byte[] buf;
ByteArrayInputStream bis;
String str="canglaoshi.avincontent,content,content";
buf=str.getBytes();
bis=new ByteArrayInputStream(buf);
UploadThread ut=new UploadThread();
ut.down(bis);
}
}
//完成各个传输任务的子线程
class UploadThread implements Runnable{
Socket socket;
public UploadThread(){}
public UploadThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
InputStream in;
try {
in = socket.getInputStream();
down(in);
//异常处理
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//测试代码
}//End of run
public void setSocket(Socket socket){
this.socket=socket;
}
//下载方法
public void down(InputStream in) throws IOException{
//局部变量
char ch;
char[] nameArr=new char[256];
byte[] buf=new byte[1024];
String name="";
OutputStream out = null;
//业务逻辑
try {
//第一步:获取文件名,构造文件输出流
int i=0;
while((ch=(char) in.read())!='n'){
nameArr[i++]= ch;
}
//name=nameArr.toString();//这句话无法将字符数组转换为字符串,需用下面的语句
name=new String(nameArr);
System.out.println("要下载的文件为:"+name);
out=new FileOutputStream("src\down\"+name);
//第二步:将输入流中的其他内容写入到文件
int len;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
out.flush();
//异常捕获
} catch (IOException e) {
e.printStackTrace();
//关闭资源
}finally{
//疑问:两个捕获可不可以放到一块呢,怎样处理关闭流时的异常最好呢?
in.close();
out.close();
}
//调试
System.out.println(name);
}
}//End of UploadThread
文件上传客户端:
public class UploadClient {
public static void main(String[] args) {
UploadClient client=new UploadClient();
client.upload("src\thursday\AsListTest.java");
}
private void upload(String name){
Socket socket=null;
OutputStream out;
try {
socket=new Socket("127.0.0.1", 55555);
out=socket.getOutputStream();
write2OutputStream(name, out);
//异常捕获
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testUpload() {
upload("src\status.xml");
}
private void write2OutputStream(String path,OutputStream out) throws IOException{
FileInputStream fis = null;
byte[] buf=new byte[1024];
String fileName="";
//业务逻辑
try {
//1.写入文件名
fileName=path.substring(path.lastIndexOf('\')+1);
System.out.println("您要上传的文件名为:"+fileName);
out.write(fileName.getBytes());
out.write('n');
//2.写入文件内容
fis=new FileInputStream(path);
int len;
while((len=fis.read(buf))!=-1){
out.write(buf, 0, len);
}
out.flush();
//异常处理
} catch (IOException e) {
e.printStackTrace();
//关闭资源
} finally{
fis.close();
out.close();
}
}//End of upload
@Test
public void testWrite2OutputStream() throws IOException {
ByteArrayOutputStream out = null;
try {
out=new ByteArrayOutputStream();
write2OutputStream("src\status.xml", out);
System.out.println(out.toString("utf-8"));
} catch (IOException e) {
e.printStackTrace();
} finally{
out.close();
}
}
}
本文已被整理到了《Java上传操作技巧汇总》,欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



