In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces Java based on BIO how to achieve file upload function, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Client package com.qst.file;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStream;import java.net.Socket Public class Client {public static void main (String [] args) {try {/ / get the input stream FileInputStream stream = new FileInputStream ("E:\\ Java backend development path\\ IO\\ handout\\ a.txt"); / / request to establish a connection with the server Socket socket = new Socket ("127.0.0.1", 9999) / / the byte output stream is packaged as a data output stream DataOutputStream outputStream = new DataOutputStream (socket.getOutputStream ()); outputStream.writeUTF (".txt"); int len; byte [] b = new byte [1024]; while ((len = stream.read (b)) > 0) {outputStream.write (b, 0, len);} outputStream.flush () / / notify the server that the data has been sent to socket.shutdownOutput ();} catch (Exception e) {/ / TODO: handle exception} server package com.qst.file;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class Server {public static void main (String [] args) throws IOException {/ / build server ServerSocket socket = new ServerSocket (9999); Socket accept = socket.accept () / / leave it to a separate thread to handle the file communication requirements with this client. New ServerThread (accept) .start ()
Server-side threading
Package com.qst.file;import java.io.DataInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.net.Socket;import java.util.UUID;public class ServerThread extends Thread {private Socket socket; public ServerThread (Socket accept) {this.socket = accept } @ Override public void run () {try {/ / get a data input stream to read the data sent by the client DataInputStream stream = new DataInputStream (socket.getInputStream ()); / / read the file type sent by the client String suffix = stream.readUTF (); System.out.println ("the server has successfully received the file type:" + suffix) / / define a byte output pipeline responsible for writing out the file data sent by the client FileOutputStream stream2 = new FileOutputStream ("E:\\ Java back-end development path\\ IO\\ handout\\ b.txt" + UUID.randomUUID (). ToString () + suffix); / / read the file data from the data input stream and write it to the byte output stream to byte [] b = new byte [1024]; int len While ((len = stream.read (b)) > 0) {stream2.write (b, 0, len);} System.out.println ("operation completed"); stream2.close (); stream.close ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}
On Java based on BIO how to achieve file upload function to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.