日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【JAVA】Socket文件上传遇到的问题!~

發(fā)布時(shí)間:2023/12/31 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【JAVA】Socket文件上传遇到的问题!~ 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

同事做的socket通信上傳文件有問題。讓我改。發(fā)現(xiàn)每次得到文件名稱。但是流已經(jīng)寫入了。文件名是第一次上傳的文件名字。網(wǎng)上看了下就這樣寫的。也不知道原因,這里把錯(cuò)誤代碼貼下。大家?guī)臀铱聪率裁丛颉!!P麓a是網(wǎng)上找到的創(chuàng)建了個(gè)線程就好了。

服務(wù)器端:

錯(cuò)誤代碼:

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監(jiān)聽。server = new ServerSocket(port);while ( (socket = server.accept()) != null) {// 建立socket輸入流 DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket .getInputStream())); // 緩沖區(qū)大小 int bufferSize = 8192; // 緩沖區(qū) 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("開始接收文件!"); // 獲取文件,下邊是進(jìn)度條。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) {//函數(shù)運(yùn)行之前要指定待傳送文件地址。/*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.net.ServerSocket; import java.net.Socket;public class FileServer {protected int listenPort = 9600;public static void main(String[] args) {FileServer server = new FileServer();server.acceptConnections();}/*** 建立鏈接* */public void acceptConnections() {try {ServerSocket server = new ServerSocket(listenPort);Socket socket = null;while (true) {socket = server.accept();// handleConnection(socket);new ServerThread(socket).start();}} catch (Exception e) {System.out.println("Unable to bind to port " + listenPort);}} }
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("接收消息緩存錯(cuò)誤\n");return;}try {// 本地保存路徑,文件名會自動(dòng)從服務(wù)器端繼承而來最好是web工程里的一個(gè)路徑。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("接收消息錯(cuò)誤" + "\n");return;}} catch (Exception e) {System.out.println("Error handling a client: " + e);}}}
客戶端代碼:


package com.zssoft.mis.struts2.util;import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.Socket;public class FileClient {public void sendData(String filepath, String IP, int port) {int progress = 0;// socket輸出流 DataOutputStream os = null; // 文件輸入流 DataInputStream is = null; // 建立socket連接 Socket socket = null; try { // 選擇進(jìn)行傳輸?shù)奈募? File file = new File(filepath); // 建立socket連接socket = new Socket(IP, port);os = new DataOutputStream(socket.getOutputStream()); // 將文件名及長度傳給服務(wù)器端。os.writeUTF(file.getName()); os.flush(); os.writeLong((long) file.length()); os.flush(); is = new DataInputStream(new BufferedInputStream( new FileInputStream(filepath))); // 緩沖區(qū)大小 int bufferSize = 8192; // 緩沖區(qū) byte[] buf = new byte[bufferSize]; // 傳輸文件 while (true) { int read = 0; if (is != null) { read = is.read(buf); } progress += Math.abs(read);if (read == -1) { break; } os.write(buf, 0, read); //發(fā)送進(jìn)度。System.out.println("文件已發(fā)送:" + (int)(100.0*progress/file.length()) + "%");} os.flush(); System.out.println("\n文件已上傳完畢!");}catch (IOException e) { e.printStackTrace(); } finally { // 關(guān)閉所有連接 try { if (os != null) os.close(); } catch (IOException e) { } try { if (is != null) is.close(); } catch (IOException e) { } try { if (socket != null) socket.close(); } catch (IOException e) { } } } public static void main(String[] args) {new FileClient().sendData("D:\\aa.swf", "10.78.151.188", 9600);}} 源代碼下載: http://download.csdn.net/detail/hateson/6198049


轉(zhuǎn)帖請注明原帖地址:http://blog.csdn.net/hateson/article/details/10927871



總結(jié)

以上是生活随笔為你收集整理的【JAVA】Socket文件上传遇到的问题!~的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。