1,安装nginx,看我之前的文章:在Linux下安装配置nginx
2,安装ftp组件,看我之前的文章:在linux中开启ftp服务
3,在usr/local/nginx/html下创建images文件夹 mkdir /usr/local/nginx/html/images
4,修改nginx.conf配置 vim /usr/local/nginx/cong/nginx.conf
5,在home的ftp用户下创建www/images文件夹 比如我的就是test用户
mkdir /home/test/www/images
6,用ftp用户权限 chown test /home/test
chmod 777 -R /home/test
7,开启nginx ./nginx
8,测试
9,到这里我们的所有的准备就完成,接下来就用java代码实现上传,我就不解释了,直接上代码
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form id="itemForm"action="/foreach/junde/saveImage.action" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>图片名称</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>商品图片</td>
<td>
<input type="file" name="picFile"/>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
controller
package com.foreach.controller;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import com.foreach.pojo.Image;
import com.foreach.service.ImagesService;
import com.foreach.utils.FtpUtil;
@Controller
@RequestMapping("/junde")
public class ImagesController {
@Autowired
private ImagesService imagesService;
@RequestMapping("/tiao")
public String tiao(){
return "images";
}
@RequestMapping(value="/saveImage",method=RequestMethod.POST)
public String saveImage(MultipartFile picFile,String name,Model model) throws Exception{
Image image = new Image();
UUID uuid = UUID.randomUUID();
String oldname = picFile.getOriginalFilename();
String newname =uuid+ oldname.substring(oldname.lastIndexOf("."));
boolean b = FtpUtil.uploadFile("192.168.60.128", 21, "test","test",
"/home/test/www/images","/4/29", newname, picFile.getInputStream());
image.setName(name);
image.setUrl("http://192.168.60.128/images/4/29/"+newname);
imagesService.saveImage(image);
if(b){
model.addAttribute("error", 1);
model.addAttribute("url", "http://192.168.60.128/images/4/29/"+newname);
}else{
model.addAttribute("error",0);
model.addAttribute("错误信息", "上传失败");
}
return "result";
}
}
service层没有什么就是调用dao层就不帖出来了
dao层
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.foreach.mapper.ImagesMapper" >
<insert id="saveImage" parameterType="image">
insert into image (name,url) values(#{name},#{url})
</insert>
</mapper>
pojo
package com.foreach.pojo;
public class Image {
private int id;
private String url;
private String name;
public Image() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
10,到这里就大功告成了,就可以运行,上传图片了。我这里用了FtpUtil工具,想要的可以私信我,虽然也没人看。。。QQ2040931220