JAva socketファイルアップロードダウンロード
5501 ワード
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FileDown extends Thread {
//
private String fileDir;
// socket
private int port;
//
private boolean stop;
public String getFileDir() {
return fileDir;
}
public void setFileDir(String fileDir) {
this.fileDir = fileDir;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public static void main(String[] args) {
FileDown fd = new FileDown();
fd.setFileDir("e:\\");
fd.setPort(9005);
fd.start();
}
/**
*
*/
@Override
public void run() {
Socket socket = null;
try {
ServerSocket ss = new ServerSocket(port);
do {
socket = ss.accept();
// public Socket accept() throws
// IOException 。 。
System.out.println(" socket ");
DataInputStream inputStream = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
// , 。
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
long passedlen = 0;
long len = 0;
//
String file=fileDir + inputStream.readUTF();
DataOutputStream fileOut = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file)));
len = inputStream.readLong();
System.out.println(" :" + len + "
");
System.out.println(" !" + "
");
while (true) {
int read = 0;
if (inputStream != null) {
read = inputStream.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
// prograssBar , ,
System.out.println(" " + (passedlen * 100 / len)
+ "%
");
fileOut.write(buf, 0, read);
}
System.out.println(" , " + file + "
");
fileOut.close();
} while (!stop);
} catch (Exception e) {
System.out.println(" " + "
");
e.printStackTrace();
return;
}
}
}
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FileUp{
//
private String filePath;
// socket
private String host;
private int port;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public static void main(String[] args) {
FileUp fu = new FileUp();
fu.setHost("127.0.0.1");
fu.setPort(9005);
fu.setFilePath("f:\\soft\\");
fu.uploadFile("DbVisualizer.rar");
}
/**
*
* @param fileName
*/
public void uploadFile(String fileName) {
Socket s = null;
try {
s = new Socket(host, port);
//
File fi = new File(filePath + fileName);
System.out.println(" :" + (int) fi.length());
DataInputStream fis = new DataInputStream(new FileInputStream(filePath + fileName));
DataOutputStream ps = new DataOutputStream(s.getOutputStream());
// 。 , , , Think In
// Java
// 4th 。
//design pattern <<Head First Design Pattern>><<Head First >><<Head First Java>>
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
// socket , server ,
// socket , 。
fis.close();
ps.close();
s.close();
System.out.println(" ");
} catch (Exception e) {
e.printStackTrace();
}
}
}