【JAVA】Socket文件上传遇到的问题!~
生活随笔
收集整理的這篇文章主要介紹了
【JAVA】Socket文件上传遇到的问题!~
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
同事做的socket通信上傳文件有問題。讓我改。發現每次得到文件名稱。但是流已經寫入了。文件名是第一次上傳的文件名字。網上看了下就這樣寫的。也不知道原因,這里把錯誤代碼貼下。大家幫我看下什么原因。。。新代碼是網上找到的創建了個線程就好了。
服務器端:
錯誤代碼:
package com.zssoft.mis.struts2.util;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 FileServer {private ServerSocket server = null;Socket socket = null;public void getData(String savePath,int port) {int progress = 0;try {// 建立socket監聽。server = new ServerSocket(port);while ( (socket = server.accept()) != null) {// 建立socket輸入流 DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket .getInputStream())); // 緩沖區大小 int bufferSize = 8192; // 緩沖區 byte[] buf = new byte[bufferSize]; int passedlen = 0; long len = 0; // 獲取文件名稱if( !savePath.contains("."))savePath += inputStream.readUTF(); DataOutputStream fileOut = new DataOutputStream( new BufferedOutputStream(new BufferedOutputStream( new FileOutputStream(savePath)))); // 獲取文件長度 len = inputStream.readLong(); System.out.println("文件的長度為:" + len + " KB"); System.out.println("開始接收文件!"); // 獲取文件,下邊是進度條。System.out.print("#>>>>>>>>#>>>>>>>>>#>>>>>>>>>#>>>>>>>>>#>>>>>>>>>#");System.out.println(">>>>>>>>>#>>>>>>>>>#>>>>>>>>>#>>>>>>>>>#>>>>>>>>>#"); while (true) { int read = 0; if (inputStream != null) { read = inputStream.read(buf); } passedlen += read;if (read == -1) { break; }if((int)(passedlen * 100.0 / len)-progress > 0){progress = (int)(passedlen * 100.0 / len); // System.out.println("文件接收了" + progress + "%"); System.out.print(">");}fileOut.write(buf, 0, read); } System.out.println();System.out.println("接收完成,文件存為: " + savePath); fileOut.close(); } }catch (Exception e){System.err.println("File Server Excetption: " + e);e.printStackTrace();} }public static void main(String[] args) {//函數運行之前要指定待傳送文件地址。/*if(args.length != 2){System.err.println("Usage: FileServer <save path> <port>");System.exit(-1);}*/new FileServer().getData("F:\\", 9600); // new FileServer().getData(args[0], Integer.parseInt(args[1])); }}正確代碼:
package com.zssoft.mis.struts2.util;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.net.Socket;public class ServerThread extends Thread {private Socket socket;public ServerThread(Socket socket) {this.socket = socket;}public void run() {try {DataInputStream inputStream = null;try {inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));} catch (Exception e) {System.out.print("接收消息緩存錯誤\n");return;}try {// 本地保存路徑,文件名會自動從服務器端繼承而來最好是web工程里的一個路徑。String savePath = "F:\\";int bufferSize = 8192;byte[] buf = new byte[bufferSize];long len = 0;savePath += inputStream.readUTF();DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));len = inputStream.readLong();System.out.println("文件的長度為:" + len + "\n");System.out.println("開始接收文件!" + "\n");while (true) {int read = 0;if (inputStream != null) {read = inputStream.read(buf);}if (read == -1) {break;}// System.out.println(buf.toString());fileOut.write(buf, 0, read);}System.out.println("接收完成,文件存為" + savePath + "\n");fileOut.flush();fileOut.close();inputStream.close();} catch (Exception e) {System.out.println("接收消息錯誤" + "\n");return;}} catch (Exception e) {System.out.println("Error handling a client: " + e);}}}
客戶端代碼:
轉帖請注明原帖地址:http://blog.csdn.net/hateson/article/details/10927871
總結
以上是生活随笔為你收集整理的【JAVA】Socket文件上传遇到的问题!~的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAS入门之(四)改变数据类型
- 下一篇: 证明矩阵的秩=行秩=列秩