Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java use Socket and IO streams to upload and download files?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces Java how to use Socket and IO stream to achieve file upload and download related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this Java how to use Socket and IO stream to achieve file upload and download articles will have a harvest, let's take a look at it.

Core technology

1 、 TCP

2 、 Socket

3. FileInputStream and FileOutputStream

4. DataInputStream and DataOutputStream

5. Multithreading

Configpackage com.io14;/** * author: brother Gu * blog address: http://blog.csdn.net/lfdfhl * / public class Config {public static final String ip = "localhost"; public static final int port = 10088;}

Clientpackage com.io14;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;/** * author: brother Gu * blog address: http://blog.csdn.net/lfdfhl * / public class Client {public static void main (String arg []) {testDownload () } public static void testUpload () {String filePath = "D:" + File.separator + "beauty.jpg"; Client client = new Client (); client.uploadFile (filePath);} public static void testDownload () {Client client = new Client (); client.downloadFile () } public void uploadFile (String filePath) {try {/ / create a file object to be uploaded File file = new File (filePath); String fileName = file.getName (); long fileLength = file.length () System.out.println ("client file to be uploaded:" + fileName + ", its size is" + fileLength); / / create FileInputStream FileInputStream fileInputStream = new FileInputStream (filePath); / / create Socket object Socket socket = new Socket (Config.ip, Config.port) / / get OutputStream OutputStream outputStream = socket.getOutputStream () from Socket; / / create DataOutputStream DataOutputStream dataOutputStream = new DataOutputStream (outputStream); / / write out file name and file size dataOutputStream.writeUTF (fileName) using DataOutputStream DataOutputStream.writeLong (fileLength); dataOutputStream.flush (); / / IO stream read and write operation byte [] buf = new byte [1024 * 1]; int len = 0 While ((len = fileInputStream.read (buf))! =-1) {dataOutputStream.write (buf, 0, len);} / / release resource dataOutputStream.flush (); fileInputStream.close () Socket.close ();} catch (Exception e) {System.out.println (e.toString ()) }} public void downloadFile () {try {/ / create Socket object Socket socket = new Socket (Config.ip, Config.port); / / get InputStream InputStream inputStream = socket.getInputStream () from Socket / / create DataInputStream object DataInputStream dataInputStream = new DataInputStream (inputStream); / / get the file name and file size of the downloaded file String fileName = dataInputStream.readUTF (); long fileLength = dataInputStream.readLong () System.out.println ("client download file:" + fileName + ", its size is" + fileLength); / / the path to save the grouping file String fileDir = "D:"; String filePath = fileDir + File.separator + fileName / / create FileOutputStream object FileOutputStream fileOutputStream = new FileOutputStream (filePath); / / IO stream read and write operation byte [] buf = new byte [1024 * 1]; int len = 0 While ((len = dataInputStream.read (buf))! =-1) {fileOutputStream.write (buf, 0, len);} / / release resource fileOutputStream.flush (); fileOutputStream.close () DataInputStream.close (); socket.close ();} catch (Exception e) {System.out.println (e.toString ());}

Serverpackage com.io14;import java.net.ServerSocket;import java.net.Socket;/** * author: brother Gu * blog address: http://blog.csdn.net/lfdfhl * / public class Server {public static void main (String arg []) {testDownload ();} public static void testUpload () {Server server = new Server () Server.handleUploadFile ();} public static void testDownload () {Server server = new Server (); server.handleDownloadFile () } public void handleUploadFile () {try {/ / create ServerSocket object ServerSocket serverSocket = new ServerSocket (Config.port) While (true) {/ / receive client connection requests Socket socket = serverSocket.accept (); / / start child threads to process file upload UploadRunnableImpl uploadRunnableImpl = new UploadRunnableImpl (socket) Thread thread = new Thread (uploadRunnableImpl); thread.start ();}} catch (Exception e) {System.out.println (e.toString ()) }} public void handleDownloadFile () {try {/ / create ServerSocket object ServerSocket serverSocket = new ServerSocket (Config.port) While (true) {/ / receive client connection request Socket socket = serverSocket.accept (); / / start child threading file to download DownloadRunnableImpl downloadRunnableImpl = new DownloadRunnableImpl (socket) Thread thread = new Thread (downloadRunnableImpl); thread.start ();}} catch (Exception e) {System.out.println (e.toString ());}}

UploadRunnableImplpackage com.io14;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.Socket;/** * author: brother Gu * blog address: http://blog.csdn.net/lfdfhl * / public class UploadRunnableImpl implements Runnable {private Socket socket; public UploadRunnableImpl (Socket socket) {this.socket = socket } @ Override public void run () {try {/ / get InputStream InputStream inputStream = socket.getInputStream () from Socket; / / create DataInputStream object DataInputStream dataInputStream = new DataInputStream (inputStream) / / get the file name and file size of the uploaded file String fileName = dataInputStream.readUTF (); long fileLength = dataInputStream.readLong (); System.out.println ("the server receives the uploaded file:" + fileName+ ", its size is:" + fileLength) / / the path to save the grouping file String fileDir = "E:"; String filePath= fileDir + File.separator+fileName; / / create FileOutputStream object FileOutputStream fileOutputStream = new FileOutputStream (filePath) / / IO stream read and write operation byte [] buf = new byte [1024x1]; int len = 0 While ((len = dataInputStream.read (buf))! =-1) {fileOutputStream.write (buf, 0, len);} / / release resource fileOutputStream.flush () FileOutputStream.close (); dataInputStream.close (); socket.close ();} catch (Exception e) {System.out.println (e.toString ());}

DownloadRunnableImplpackage com.io14;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.OutputStream;import java.net.Socket;/** * author: brother Gu * blog address: http://blog.csdn.net/lfdfhl * / public class DownloadRunnableImpl implements Runnable {private Socket socket; public DownloadRunnableImpl (Socket socket) {this.socket = socket } @ Override public void run () {try {/ / create the file object to be downloaded String filePath = "E:" + File.separator+ "beauty.jpg"; File file = new File (filePath); String fileName = file.getName () Long fileLength = file.length (); System.out.println ("server download file:" + fileName + "with the size of" + fileLength); / / create FileInputStream FileInputStream fileInputStream = new FileInputStream (filePath) / / get OutputStream OutputStream outputStream = socket.getOutputStream () from Socket; / / create DataOutputStream DataOutputStream dataOutputStream = new DataOutputStream (outputStream); / / write out file name and file size dataOutputStream.writeUTF (fileName) using DataOutputStream DataOutputStream.writeLong (fileLength); dataOutputStream.flush (); / / IO stream read and write operation byte [] buf = new byte [1024 * 1]; int len = 0 While ((len = fileInputStream.read (buf))! =-1) {dataOutputStream.write (buf, 0, len);} / / release resource dataOutputStream.flush (); fileInputStream.close () Socket.close ();} catch (Exception e) {System.out.println (e.toString ());}

This is the end of the article on "how Java uses Socket and IO streams to upload and download files". Thank you for reading! I believe you all have a certain understanding of "how Java uses Socket and IO streams to upload and download files". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report