java upload file to another computer in the local area network

I encountered another problem today: how to upload files to another machine in the local area network. The solutions are roughly as follows:
1. Build an FTP server, and then transfer files through the FTP protocol. This method is effective, but a little troublesome.
2. SocketTo transfer files through this method, you need to write a client and a server. This scheme is more general, and this scheme is good when other schemes are not feasible.
3. Build an HTTP server (such as tomcat ), upload files directly to this machine when uploading files, you need to deploy the application for uploading files.
4. If it is a windows system, it can take the simplest form to complete file transfer (file sharing).

This article uses the fourth method to complete this function:
first, set up file sharing on the computer where you want to save the uploaded files. For example, this article shares the image folder:
right-click the image folder -> Properties -> Sharing -> Files And folder sharing
File Sharing
Note: Be sure to add Evenyoneusers and give 读写permissions.
After the sharing is complete, start -> run, enter \\192.168.0.67\image (enter your own ip according to the actual situation) to open the shared file directory.
write picture description here
Following is the java code:

public class Test {
    public static void main(String[] args) throws Exception {
        String srcPath = "c:\\pic.jpg";
        File parentDir = new File("\\\\192.168.0.67\\image");
        File targetPath = new File(parentDir, 
                UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");
        InputStream in = new FileInputStream(srcPath);
        OutputStream out = new FileOutputStream(targetPath);
        try {
            byte[] bs = new byte[1024];
            int len = -1;
            while((len = in.read(bs)) != -1) {
                out.write(bs, 0, len);
            }
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("上传成功!!");
    }
}

  This example just uploads a local image file to the computer in the local area network. File parentDir = new File("\\\\192.168.0.67\\image") Pay attention to the file path behind. \\\\ is followed by the local area network IP address, followed by the directory of the shared file. Next, operate the same as ordinary files. , write to the destination via the stream. The file name is generated by UUID, remove it -, about the file directory breakup: you can generate the file directory through some algorithms, for example: yyyy/MM/ddcreate a folder through the date format, or you can do some things through the hash code of the file name Process to generate the catalog.
  As you can see from the image below, the image has indeed been uploaded:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325470908&siteId=291194637