3个回答
展开全部
和发送普通的文件没什么区别 只是取过来 显示的问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
MB~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
FileClient:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class FileClient extends Thread {
private Socket socket;
private InetAddress ip;
private int port;
File file;
public FileClient(File file,InetAddress ip,int port){
this.file = file;
this.ip = ip;
this.port = port;
socket = null;
}
public void run() {
try {
socket = new Socket(ip,port);
FileInputStream fis = new FileInputStream(file);
byte[] buff = new byte[fis.available()];
fis.read(buff);
OutputStream os = socket.getOutputStream();
os.write(buff);
os.flush();
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileServer:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFileChooser;
public class FileServer extends Thread{
private ServerSocket server;
private Socket socket;
private boolean flag;
public FileServer(){
server = null;
socket = null;
flag = true;
}
public void run(){
try {
server = new ServerSocket(8088);
while(flag){
socket = server.accept();
JFileChooser chooser = new JFileChooser("D:/");
int result = chooser.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
InputStream is = socket.getInputStream();
OutputStream os = new FileOutputStream(file);
byte[] buff = new byte[is.available()];
is.read(buff);
os.write(buff);
os.flush();
is.close();
os.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class FileClient extends Thread {
private Socket socket;
private InetAddress ip;
private int port;
File file;
public FileClient(File file,InetAddress ip,int port){
this.file = file;
this.ip = ip;
this.port = port;
socket = null;
}
public void run() {
try {
socket = new Socket(ip,port);
FileInputStream fis = new FileInputStream(file);
byte[] buff = new byte[fis.available()];
fis.read(buff);
OutputStream os = socket.getOutputStream();
os.write(buff);
os.flush();
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileServer:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFileChooser;
public class FileServer extends Thread{
private ServerSocket server;
private Socket socket;
private boolean flag;
public FileServer(){
server = null;
socket = null;
flag = true;
}
public void run(){
try {
server = new ServerSocket(8088);
while(flag){
socket = server.accept();
JFileChooser chooser = new JFileChooser("D:/");
int result = chooser.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
InputStream is = socket.getInputStream();
OutputStream os = new FileOutputStream(file);
byte[] buff = new byte[is.available()];
is.read(buff);
os.write(buff);
os.flush();
is.close();
os.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询