Java文件传输(有进度条)
生活随笔
收集整理的這篇文章主要介紹了
Java文件传输(有进度条)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Tcp連接實現文件傳輸,使用ServcerScoket與Scoket類實現服務器與客戶端建立連接考慮美觀問題用idea的JfromDeSigener設計界面,雖然也不怎么美觀...
客戶端
文件選擇部分
private void selectFileMouseClicked(MouseEvent e) {// TODO add your code hereJFileChooser jFileChooser = new JFileChooser();//創建文件選擇器jFileChooser.showOpenDialog(this);//顯示文件打開框if(jFileChooser.getSelectedFile() != null)//選擇了文件{file = jFileChooser.getSelectedFile();filemess.setText("");//清空之前顯示的文件名filemess.setText(file.getName());}}文件傳輸部分
整的比較花里胡哨,加了個服務端接收拒絕的功能 upprobar.setString(null);//設置進度條內容為空upprobar.setStringPainted(true);//設置進度條顯示百分比//獲取ip地址String ip = ip_text.getText().trim();//獲取端口號String port_str = port_text.getText().trim();int port = 0;if(port_str != null && (!("".equals(port_str)))){port = Integer.parseInt(port_str);System.out.println(port);}if(ip != null && port != 0 && !("".equals(ip))) {try {//創建socketsocket = new Socket(ip,port);} catch (IOException ioException) {ioException.printStackTrace();}new Thread(new Runnable() {@Overridepublic void run() {FileUp();}}).start();}else {JOptionPane.showMessageDialog(this,"請輸入IP地址或端口號");}private void FileUp() {double percentage = 0;//百分比OutputStream outputStream = null;FileInputStream fileInputStream = null;DataOutputStream dataOutputStream = null;try {long AllSize = file.length();//獲取文件的大小,單位字節outputStream = socket.getOutputStream();fileInputStream = new FileInputStream(file);DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());dataOutputStream = new DataOutputStream(socket.getOutputStream());String receflag = dataInputStream.readUTF();if("接收".equals(receflag)){long read_size = 0;byte[] filebuffeer = new byte[1024];int len = 0;//每次上傳的大小//發送文件名dataOutputStream.writeUTF(file.getName());//發送文件大小dataOutputStream.writeLong(AllSize);while((len = fileInputStream.read(filebuffeer)) != -1){read_size += len;//計算當前傳輸的總字節數outputStream.write(filebuffeer,0,len);outputStream.flush();percentage = (double) read_size / AllSize;//計算當前傳輸進度upprobar.setValue((int) (percentage * 100));}upprobar.setString("上傳完成");}else if("拒絕接收".equals(receflag)){JOptionPane.showMessageDialog(this,"服務端拒絕接收");}System.out.println(socket);// fileInputStream.close();} catch (IOException ioException) {ioException.printStackTrace();}finally {if(fileInputStream != null){try {fileInputStream.close();System.out.println("文件輸入流關閉");} catch (IOException e) {e.printStackTrace();}}if(outputStream != null){try {outputStream.close();System.out.println("輸出流關閉");} catch (IOException e) {e.printStackTrace();}}if(dataOutputStream != null){try {dataOutputStream.close();System.out.println("數據輸出流關閉");} catch (IOException e) {e.printStackTrace();}}}}服務端
比較簡陋,湊合看吧,又不是不能用,哈哈 private void Receive_File() {Socket socket = null;InputStream inputStream = null;DataInputStream dataInputStream = null;FileOutputStream fileOutputStream = null;DataOutputStream dataOutputStream = null;try {socket = serverSocket.accept();System.out.println("有新客戶端接入");dataOutputStream = new DataOutputStream(socket.getOutputStream());int rece_flag = JOptionPane.showConfirmDialog(this,"有新文件,是否接受文件","",JOptionPane.YES_NO_OPTION);if(rece_flag == 0){JFileChooser jFileChooser = new JFileChooser();//創建文件選擇器jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//可以選擇文件與文件夾int saveflag = jFileChooser.showSaveDialog(this);//顯示文件保存對話框if(saveflag == JFileChooser.APPROVE_OPTION) {dataOutputStream.writeUTF("接收");String filepath = jFileChooser.getSelectedFile().getAbsolutePath();inputStream = socket.getInputStream();dataInputStream = new DataInputStream(socket.getInputStream());down_pro.setString(null);down_pro.setStringPainted(true);File file = new File(filepath + dataInputStream.readUTF());//通過選擇的文件路徑+傳輸的文件名,確定文件保存位置fileOutputStream = new FileOutputStream(file);long read_size = 0;double percentage = 0;//百分比long filesize = dataInputStream.readLong();//獲取文件大小int len = 0;//每次接收的大小byte[] bytes = new byte[1024];//下邊這部分,基本跟客戶端一樣while((len = inputStream.read(bytes)) != -1){read_size += len;fileOutputStream.write(bytes,0,len);fileOutputStream.flush();percentage = (double) read_size / filesize;down_pro.setValue((int)(percentage * 100));// System.out.println((int)(percentage * 100));if(read_size == filesize){JOptionPane.showMessageDialog(this,"接收完成");}}down_pro.setString("接收完成");}}else if(rece_flag == 1){dataOutputStream.writeUTF("拒絕接收");}} catch (IOException e) {e.printStackTrace();}finally {if(fileOutputStream != null){try {fileOutputStream.close();System.out.println("文件輸出流關閉");} catch (IOException e) {e.printStackTrace();}}if(inputStream != null){try {inputStream.close();System.out.println("輸入流關閉");} catch (IOException e) {e.printStackTrace();}}if(dataInputStream != null){try {dataInputStream.close();System.out.println("數據輸入流關閉");} catch (IOException e) {e.printStackTrace();}}if(socket != null){try {socket.close();System.out.println("socket關閉");} catch (IOException e) {e.printStackTrace();}}}}總結
以上是生活随笔為你收集整理的Java文件传输(有进度条)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为何选用F1值(调和平均数)衡量P与R?
- 下一篇: 超级玛丽制作揭秘8跟踪打印 类FILER