ftp服务器搭建与图片上传

本文详细介绍了在Windows 10环境下搭建FTP服务器的过程,并提供了使用Java进行文件上传的具体实现代码,包括连接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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值