Java文件传输(有进度条)
生活随笔
收集整理的這篇文章主要介紹了
Java文件传输(有进度条)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Tcp連接實(shí)現(xiàn)文件傳輸,使用ServcerScoket與Scoket類實(shí)現(xiàn)服務(wù)器與客戶端建立連接考慮美觀問題用idea的JfromDeSigener設(shè)計(jì)界面,雖然也不怎么美觀...
客戶端
文件選擇部分
private void selectFileMouseClicked(MouseEvent e) {// TODO add your code hereJFileChooser jFileChooser = new JFileChooser();//創(chuàng)建文件選擇器jFileChooser.showOpenDialog(this);//顯示文件打開框if(jFileChooser.getSelectedFile() != null)//選擇了文件{file = jFileChooser.getSelectedFile();filemess.setText("");//清空之前顯示的文件名filemess.setText(file.getName());}}文件傳輸部分
整的比較花里胡哨,加了個(gè)服務(wù)端接收拒絕的功能 upprobar.setString(null);//設(shè)置進(jìn)度條內(nèi)容為空upprobar.setStringPainted(true);//設(shè)置進(jìn)度條顯示百分比//獲取ip地址String ip = ip_text.getText().trim();//獲取端口號(hào)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 {//創(chuàng)建socketsocket = new Socket(ip,port);} catch (IOException ioException) {ioException.printStackTrace();}new Thread(new Runnable() {@Overridepublic void run() {FileUp();}}).start();}else {JOptionPane.showMessageDialog(this,"請(qǐng)輸入IP地址或端口號(hào)");}private void FileUp() {double percentage = 0;//百分比OutputStream outputStream = null;FileInputStream fileInputStream = null;DataOutputStream dataOutputStream = null;try {long AllSize = file.length();//獲取文件的大小,單位字節(jié)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;//每次上傳的大小//發(fā)送文件名dataOutputStream.writeUTF(file.getName());//發(fā)送文件大小dataOutputStream.writeLong(AllSize);while((len = fileInputStream.read(filebuffeer)) != -1){read_size += len;//計(jì)算當(dāng)前傳輸?shù)目傋止?jié)數(shù)outputStream.write(filebuffeer,0,len);outputStream.flush();percentage = (double) read_size / AllSize;//計(jì)算當(dāng)前傳輸進(jìn)度upprobar.setValue((int) (percentage * 100));}upprobar.setString("上傳完成");}else if("拒絕接收".equals(receflag)){JOptionPane.showMessageDialog(this,"服務(wù)端拒絕接收");}System.out.println(socket);// fileInputStream.close();} catch (IOException ioException) {ioException.printStackTrace();}finally {if(fileInputStream != null){try {fileInputStream.close();System.out.println("文件輸入流關(guān)閉");} catch (IOException e) {e.printStackTrace();}}if(outputStream != null){try {outputStream.close();System.out.println("輸出流關(guān)閉");} catch (IOException e) {e.printStackTrace();}}if(dataOutputStream != null){try {dataOutputStream.close();System.out.println("數(shù)據(jù)輸出流關(guān)閉");} catch (IOException e) {e.printStackTrace();}}}}服務(wù)端
比較簡陋,湊合看吧,又不是不能用,哈哈 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();//創(chuàng)建文件選擇器jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//可以選擇文件與文件夾int saveflag = jFileChooser.showSaveDialog(this);//顯示文件保存對(duì)話框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());//通過選擇的文件路徑+傳輸?shù)奈募?#xff0c;確定文件保存位置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("文件輸出流關(guān)閉");} catch (IOException e) {e.printStackTrace();}}if(inputStream != null){try {inputStream.close();System.out.println("輸入流關(guān)閉");} catch (IOException e) {e.printStackTrace();}}if(dataInputStream != null){try {dataInputStream.close();System.out.println("數(shù)據(jù)輸入流關(guān)閉");} catch (IOException e) {e.printStackTrace();}}if(socket != null){try {socket.close();System.out.println("socket關(guān)閉");} catch (IOException e) {e.printStackTrace();}}}}總結(jié)
以上是生活随笔為你收集整理的Java文件传输(有进度条)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为何选用F1值(调和平均数)衡量P与R?
- 下一篇: java怎么将文件传到服务器,Java文