ftp服务器搭建与图片上传

Win10如何搭建FTP服务器

https://jingyan.baidu.com/article/0bc808fc408fa91bd585b94f.html

ftp 之 使用java将文件上传到ftp服务器上

https://www.cnblogs.com/forever2h/p/6861646.html

@RequestMapping(value = "upload",method = RequestMethod.POST)
public String upload(@RequestParam("fileName") MultipartFile multipartFile){
    InputStream inputStream = null;
    InputStream local = null;
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect("172.16.0.112", 21);
        ftpClient.login("ftp", "000000");
        String path = "/test";
        boolean dictoryExitFlag = ftpClient.changeWorkingDirectory(path);
        if (!dictoryExitFlag){
            ftpClient.makeDirectory(path);
        }
        ftpClient.changeWorkingDirectory(path);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        File file = new File("H:/图片/timg.jpg");
        local = new FileInputStream(file);

        inputStream = multipartFile.getInputStream();

        String name = file.getName();
        String suffix=name.substring(name.lastIndexOf("."));

        DateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String s = new Date().toString();
        String format1 = format.format(new Date());

        ftpClient.storeFile(format1 +suffix, inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        try {
            local.close();
            ftpClient.logout();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return "hello";

}
<form action="/vue/upload" method="post" enctype="multipart/form-data">
<input type="file" name="fileName">上传图片1</input>
    <input type="submit">
</form>

猜你喜欢

转载自blog.csdn.net/weixin_33387378/article/details/84544822