没有用多线程,这只是个例子,到时会用服务端socket开一个线程client:public class FileTransforSocketClient { public static void sendFileExt(Socket m_socket, String filePathName) { int index = filePathName.lastIndexOf("."); String ext = filePathName.substring(index + 1); ext += "\n"; try { DataOutputStream m_dataOutStr = new DataOutputStream(m_socket.getOutputStream()); m_dataOutStr.writeChars(ext); m_dataOutStr.flush(); } catch (Exception ex) { } }
public static void transforFile(Socket m_socket, final String[] filePathName) throws IOException{ for (int i = 0; i < filePathName.length; i++) { int offset = 0;
File m_file = new File(filePathName[i]); long m_fileLength = m_file.length();
sendFileExt(m_socket, filePathName[i]);
FileInputStream m_fileInStr = new FileInputStream(m_file); byte[] buffer = new byte[m_fileInStr.available()]; long m_readed = 0;
PrintStream m_ps = new PrintStream(m_socket.getOutputStream());
m_ps = new PrintStream(m_socket.getOutputStream());
那会不会是由于这个原因呢?
public static void sendFileExt(Socket m_socket, String filePathName) {
int index = filePathName.lastIndexOf(".");
String ext = filePathName.substring(index + 1);
ext += "\n";
try {
DataOutputStream m_dataOutStr = new DataOutputStream(m_socket.getOutputStream());
m_dataOutStr.writeChars(ext);
m_dataOutStr.flush();
}
catch (Exception ex) {
}
}
public static void transforFile(Socket m_socket, final String[] filePathName) throws IOException{
for (int i = 0; i < filePathName.length; i++) {
int offset = 0;
File m_file = new File(filePathName[i]);
long m_fileLength = m_file.length();
sendFileExt(m_socket, filePathName[i]);
FileInputStream m_fileInStr = new FileInputStream(m_file); byte[] buffer = new byte[m_fileInStr.available()];
long m_readed = 0;
PrintStream m_ps = new PrintStream(m_socket.getOutputStream());
m_ps = new PrintStream(m_socket.getOutputStream());
if ((m_readed = m_fileInStr.read(buffer, offset, (int)(m_fileLength - offset))) > 0) {
m_ps.write(buffer, offset, buffer.length);
m_ps.flush();
}
m_fileInStr.close();
}
m_socket.close();
}
public static void main(String[] argc) {
final String[] filePathName = {"E:\\downloads\\Other\\Advertise\\Logo\\1037844404.jpe",
"E:\\downloads\\Other\\Advertise\\Logo\\大跳艳舞的男子.gif"};
try {
Socket m_socket = new Socket(InetAddress.getLocalHost(), 34567);
if (m_socket == null) {
System.out.println("客户端socket初始化失败!");
System.exit(0);
}
transforFile(m_socket, filePathName);
}
catch (Exception ex) {
System.err.println(ex.toString());
ex.printStackTrace();
}
}
}server:public class FileTransforSocketServer {
public FileTransforSocketServer() { }
public static String trimAll(String original) {
StringBuffer m_buf = new StringBuffer();
char tempChar = ' ';
for (int i = 0; i < original.length(); i++) {
tempChar = original.charAt(i);
if ((tempChar >= 'a' && tempChar <='z') || (tempChar >= 'A' && tempChar <='Z') || (tempChar >= '0' && tempChar <='9')) {
m_buf.append(tempChar);
}
}
return m_buf.toString();
}
public static void acceptFile(Socket m_socket, String m_filePathName) {
int m_readed = 0;
int offset = 0;
m_filePathName += "test\\";
String filePathName = m_filePathName;
File file = new File(filePathName);
if (!file.exists()) {
file.mkdir();
} do {
filePathName = m_filePathName;
System.out.println (filePathName);
try {
Random name = new Random();
DataInputStream m_dataInStr = new DataInputStream(m_socket.getInputStream());
String ext = m_dataInStr.readLine();
while (ext ==null || ext.equals("")) {
ext = m_dataInStr.readLine();
}
filePathName += String.valueOf(name.nextInt());
filePathName += ".";
ext = trimAll(ext); filePathName += ext;
int m_strLength = 0;
FileOutputStream m_file = new FileOutputStream(filePathName);
while (m_socket.getInputStream().available() <= 0) {
continue;
}
m_strLength = m_socket.getInputStream().available();
byte[] buffer = new byte[m_strLength];
while ((m_readed = m_socket.getInputStream().read(buffer, offset, m_strLength)) > 0) {
if (m_readed != 1) {
m_file.write(buffer, offset, m_readed);
}else {
break;
}
}
}
catch (Exception ex) {
System.err.println("接受文件出现异常" + ex.toString());
ex.printStackTrace();
System.exit(1);
}
}while (m_readed > 0);
}
public static void main(String[] argc) {
final String filePathName = "d:\\stduio\\temp\\upload\\";
try {
ServerSocket m_servSocket = new ServerSocket(34567);
System.out.println ("socket 服务器已启动!");
while (true) {
try {
Socket m_socket = m_servSocket.accept();
if (m_socket == null) {
continue;
}else {
System.out.println("socket 已连接!");
acceptFile(m_socket, filePathName);
}
}
catch (Exception ex) {
System.err.println(ex.toString());
ex.printStackTrace();
continue;
}
}
}
catch (Exception ex) {
System.err.println(ex.toString());
ex.printStackTrace();
}
}
}
Socket m_socket = m_servSocket.accept();
放在循环的外面。
其次,在用m_fileInStr.available()的时候先调用
System.out.println(m_fileinStr.available());
看到底是几。
这样它不就只能就接受一次accept(),不能多次连接了