单独上传图片(copy 代码直接可用)

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;
        } 
        
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章