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 to realize File upload function by Java Network programming TCP

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "Java network programming TCP how to achieve file upload function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java network programming TCP how to achieve file upload function" bar!

Client: package com.kuang.lesson02;import java.io.*;import java.net.InetAddress;import java.net.Socket;// client public class TcpClientDemo2 {public static void main (String [] args) throws Exception {/ / 1, create a Socket connection Socket socket = new Socket ("127.0.0.1"), 9000); / / 2, create an output stream OutputStream os = socket.getOutputStream () / / 3. Read the file FileInputStream fis = new FileInputStream (new File ("sendFile.jpg")); / / 4, write out the file byte [] buffer = new byte [1024]; int len; while ((len=fis.read (buffer))! =-1) {os.write (buffer,0,len) } / / notify the server that I have finished socket.shutdownOutput (); / / make sure that the server has received it before disconnecting InputStream inputStream = socket.getInputStream (); / / String byte [] ByteArrayOutputStream baos = new ByteArrayOutputStream (); byte [] buffer2 = new byte [1024]; int len2 While ((len2=inputStream.read (buffer2))! =-1) {baos.write (buffer2,0,len2);} System.out.println (baos.toString ()); / / 5. Close the resource fis.close (); os.close (); socket.close ();}} server: package com.kuang.lesson02;import java.io.*;import java.net.ServerSocket Import java.net.Socket;// server public class TcpServerDemo02 {public static void main (String [] args) throws Exception {/ / 1, create service ServerSocket serverSocket = new ServerSocket (9000); / / 2, listen for client connection / / blocking listening, will always wait for client connection Socket socket = serverSocket.accept () / / 3. Get input stream InputStream is = socket.getInputStream (); / / 4, file output FileOutputStream fos = new FileOutputStream (new File ("receive.jpg")); byte [] buffer = new byte [1024]; int len; while ((len=is.read (buffer))! =-1) {fos.write (buffer,0,len) } / / notify the client that I have received OutputStream os = socket.getOutputStream (); os.write ("I have received it, you can disconnect" .getBytes ()); / / 5. Close the resource fos.close (); is.close (); socket.close (); serverSocket.close ();}} run result:

1. First of all, you can find that there are only sendFile.jsp files to be uploaded in the directory.

2. Run the server, create the service, and you can find that it is running all the time.

3. Run the client and upload the file. You can find that there is an extra file receive.jpg uploaded to the server in the directory, and the message sent by the server is received at the same time.

4. Open two files and find the same

At this point, I believe that everyone on the "Java network programming TCP how to achieve file upload function" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report