This article shares the specific code of Java file upload server and client for your reference. The specific content is as follows
File upload server:
/** * Server-side using TCP protocol to implement upload function* Ideas: * Create a new ServerSocket * Wait for the client to connect* After connecting, start the child thread, pass the Socket obtained by the connection to the child thread* Looping* @author yajun * */ public class UploadServer { public static void main(String[] args) { UploadServer server=new UploadServer(); UploadThread command=new UploadThread(); server.start(command); } /** * Function: Accept connection, start the child thread, loop* @param command The child thread object used to download, which implements the Runnable interface*/ private void start(UploadThread command){ //Local variable ServerSocket serverSocket = null; Socket transSocket; //Business logic try { serverSocket=new ServerSocket(55555); while(true){ System.out.println("wait for connection..."); transSocket=serverSocket.accept(); int i=0; i++; System.out.println("th"+i+"connection"); //Do you need to close the thread after downloading? ? ? command.setSocket(transSocket); Executors.newFixedThreadPool(5).execute(command); } //Exception catch} catch (IOException e) { e.printStackTrace(); //Close resource} finally{ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } }//End of try }//End of connect @Test public void testConnect() { //Test task: first run the server side, then run the client multiple times. The server segment can continuously create child threads, and the test is successful//Test preparation: Construct a thread to simulate the download thread UploadThread command=new UploadThread(); start(command); } @Test public void testDown() throws IOException { byte[] buf; ByteArrayInputStream bis; String str="canglaoshi.avi/ncontent,content,content"; buf=str.getBytes(); bis=new ByteArrayInputStream(buf); UploadThread ut=new UploadThread(); ut.down(bis); } } //Subthread that completes each transmission task class UploadThread implements Runnable{ Socket socket; public UploadThread(){} public UploadThread(Socket socket){ this.socket=socket; } @Override public void run() { InputStream in; try { in = socket.getInputStream(); down(in); //Exception handling} catch (IOException e) { e.printStackTrace(); } finally{ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } //Test code/*try { Thread.sleep(5000); System.out.println(Thread.currentThread().getName()+",copyName()+"); } catch (InterruptedException e) { e.printStackTrace(); }*/ }//End of run public void setSocket(Socket socket){ this.socket=socket; } //Download method/** * Goal: Write data in InputStream to the local* Idea: * 1. Get the input stream, it is best to pass it in the input stream instead of directly obtaining it from the Socket, and pass it in to use unit tests* 2. Read the file name from the input stream* 3. Create a new file and file output stream* 4. Read the file content from the input stream to the file output stream* 5. * @throws IOException */ public void down(InputStream in) throws IOException{ //Local variable char ch; char[] nameArr=new char[256]; byte[] buf=new byte[1024]; String name=""; OutputStream out = null; //Business logic try { //Step 1: Get the file name and construct the file output stream int i=0; while((ch=(char) in.read())!='/n'){ nameArr[i++]= ch; } //name=nameArr.toString();//This sentence cannot convert a character array into a string. You need to use the following statement name=new String(nameArr); System.out.println("The file to be downloaded is: "+name); out=new FileOutputStream("src//down//"+name); //Step 2: Write other contents in the input stream to the file int len; while((len=in.read(buf))!=-1){ out.write(buf,0,len); } out.flush(); //Exception capture} catch (IOException e) { e.printStackTrace(); //Close resource} finally{ //Question: Can two captures be put together? How to deal with exceptions when closing the stream? in.close(); out.close(); } //Debug System.out.println(name); } }//End of UploadThread File upload client:
/** * Client that uses TCP protocol to implement upload function* @author yajun */ public class UploadClient { public static void main(String[] args) { UploadClient client=new UploadClient(); client.upload("src//thursday//AsListTest.java"); } /** * Function: Upload file to the server* 1. Establish a connection to the server* 2. Get the output stream* 3. Write the file contents to the output stream* 4. Get the server's response*/ private void upload(String name){ Socket socket=null; OutputStream out; try { socket=new Socket("127.0.0.1", 5555); out=socket.getOutputStream(); write2OutputStream(name, out); //Exception capture} catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testUpload() { upload("src//status.xml"); } /** * Function: Pass in the file name and output stream, write the file to the output stream* @param path * @throws IOException */ private void write2OutputStream(String path,OutputStream out) throws IOException{ FileInputStream fis = null; byte[] buf=new byte[1024]; String fileName=""; //Business logic try { //1. Write the file name fileName=path.substring(path.lastIndexOf('//')+1); System.out.println("The file name you want to upload is: "+fileName); out.write(fileName.getBytes()); out.write('/n'); //2. Write the file content fis=new FileInputStream(path); int len; while((len=fis.read(buf))!=-1){ out.write(buf, 0, len); } out.flush(); //Exception handling} catch (IOException e) { e.printStackTrace(); //Close the resource} finally{ fis.close(); out.close(); } }//End of upload @Test public void testWrite2OutputStream() throws IOException { ByteArrayOutputStream out = null; try { out=new ByteArrayOutputStream(); write2OutputStream("src//status.xml", out); System.out.println(out.toString("utf-8")); } catch (IOException e) { e.printStackTrace(); } finally{ out.close(); } } }This article has been compiled into "Summary of Java Upload Operation Techniques", and everyone is welcome to learn and read.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.