import java.io.File;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/ **
*
單獨上傳圖片* @author hxw
*
* /
@Controller
public class sctp {
@RequestMapping(value = {“uploadFile.html”},method = RequestMethod.POST)
public String upload(User user,HttpServletRequest request,Model model)throws Exception {
System.out.println(request.getParameter(“name”)) ;
//保存數據庫的路徑
String sqlPath = null;
//定義文件保存的本地路徑
String localPath =“E:/ files /”;
//定義文件名
String filename = null;
if(!user.getFile()。isEmpty()){
//生成uuid作爲文件名稱
String uuid = UUID.randomUUID()。toString()。replaceAll(“ - ”,“”);
//獲得文件類型(可以判斷如果不是圖片,禁止上傳)
String contentType = user.getFile()。getContentType();
//獲得文件後綴名
String suffixName = contentType.substring(contentType.indexOf(“/”)+ 1);
//得到文件名
filename = uuid +“。”+ suffixName;
的System.out.println(文件名);
//文件保存路徑
// user.getFile()。transferTo(new File(localPath + filename));
//二者只能留一個
//壓縮文件
// https://mp.csdn.net/postedit/84561127
ImageHelper.compress(user.getFile()。getInputStream(),new File(localPath + filename),700);
}
model.addAttribute(“user”,user);
返回“../../index”;
}
}
import org.springframework.web.multipart.MultipartFile;
public class User {
private Integer id;
私有字符串名稱;
private String pwd;
私有字符串圖像;
私有的MultipartFile文件;
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPwd(){
return pwd;
}
public void setPwd(String pwd){
this.pwd = pwd;
}
public String getImage(){
return image;
}
public void setImage(String image){
this.image = image;
}
public MultipartFile getFile(){
return file;
}
public void setFile(MultipartFile file){
this.file = file;
}
}