vsftpd + nginx 实现图片上传到服务器
- 设置图片存放路径
- vim /usr/local/nginx/conf/nginx.conf(添加下面代码)
location /images/ { root /home/ftpuser/www/; autoindex on; } # root 是将images映射到/home/ftpuser/www/ # autoindex on 是打开浏览功能
- vim /usr/local/nginx/conf/nginx.conf(添加下面代码)
- 重启nginx,加载配置文件
- vim /usr/local/nginx/sbin
- ./nginx -s reload
- 修改用户访问权限
- chown ftpuser /home/ftpuser
- chmod 777 -R /home/ftpuser
- 浏览器访问
- 域名或IP地址 / images / xxx.jpg
- java实现ftp上传文件测试
public class TestFTP { @Test public void testFtpClient() throws Exception{ // 创建一个 FtpClient 对象 FTPClient ftpClient = new FTPClient(); // 创建 ftp连接 // xxxx : IP地址 ftpClient.connect("xxxx", 21); // 登录 ftp 服务器,使用用户名和密码 ftpClient.login("ftpuser", "123456"); // 上传文件 // 读取本地文件 FileInputStream inputStream = new FileInputStream(new File("E:\\imageShow\\local.jpg")); // 设置上传路径 // 服务器路径 ftpClient.changeWorkingDirectory("/home/ftpuser/www/images"); // 修改上传文件的格式(FTP:文本格式 图片:二进制格式) ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 第一个参数:上传到服务器端的文件名 // 第二个参数:上传文件的 inputStream ftpClient.storeFile("server.jpg", inputStream); // 关闭连接 ftpClient.logout(); } }
- 使用配置文件设置FTP和图片服务器的参数
- 新建一个 resource.properties
# FTP 相关配置 # FTP 相关地址 # 即 IP地址 FTP_ADDRESS=xxx.xxx.xxx.xxx FTP_port=21 FTP_USERNAME=ftpuser FTP_PASSWORD=123456 FTP_BASE_PATH=/home/ftpuser/www/images # 图片服务器相关配置 # 图片服务器基础 url IMAGE_BASE_URL=http://xxx.xxx.xxx.xxx/images
- 在spring配置文件中添加扫描
<context:property-placeholder location="classpath:resource/*.properties" />
- 在类中使用
@Value("${FTP_ADDRESS}") private String FTP_ADDRESS; @Value("${FTP_port}") private Integer FTP_port;
- 新建一个 resource.properties
- 上传图片
-
service
/** * @Description: 图片上传服务 * @Author: 尘 * @Date: 2019/2/2 */ @Service public class PictureServiceImpl implements PictureService{ @Value("${FTP_ADDRESS}") private String FTP_ADDRESS; @Value("${FTP_port}") private Integer FTP_port; @Value("${FTP_USERNAME}") private String FTP_USERNAME; @Value("${FTP_PASSWORD}") private String FTP_PASSWORD; @Value("${FTP_BASE_PATH}") private String FTP_BASE_PATH; @Value("${IMAGE_BASE_URL}") private String IMAGE_BASE_URL; @Override public Map uploadPicture(MultipartFile uploadFile){ Map resultMap = new HashMap(); try { // 生成一个新文件名 // 取原文件名 String oldName = uploadFile.getOriginalFilename(); // 生成新文件名 // UUID.randomUUID String newName = IDUtils.genImageName(); // 截扩展名 newName = newName + oldName.substring(oldName.lastIndexOf(".")); // 图片上传 String imagePath = new DateTime().toString("/yyyy/MM/dd"); boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_port,FTP_USERNAME, FTP_PASSWORD, FTP_BASE_PATH, new DateTime().toString("/yyyy/MM/dd"), newName, uploadFile.getInputStream()); // 返回结果 if(!result){ // 失败 resultMap.put("error", 1); resultMap.put("message", "文件上传失败"); return resultMap; } resultMap.put("error", 0); resultMap.put("url", IMAGE_BASE_URL + "/" + imagePath + "/" + newName); return resultMap; } catch (Exception e) { resultMap.put("error", 1); resultMap.put("message", "文件上传发生异常"); return resultMap; } } }
-
controller
/** * @Description: 图片上传 * @Author: 尘 * @Date: 2019/2/2 */ @Controller public class PictureController { @Autowired private PictureService pictureService; @RequestMapping("/pic/upload") @ResponseBody public String pictureUpload(MultipartFile uploadFile){ Map result = pictureService.uploadPicture(uploadFile); // return result; 仅在谷歌浏览器有效 // 为保证功能的兼容性,需要把result转换成json格式字符串 String json = JsonUtils.objectToJson(result); return json; } }
-
springmvc.xml
<!-- 定义文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设定默认编码 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设定文件上传的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> </bean>
-