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

本文介绍了一个使用Spring MVC框架实现的图片上传功能,包括图片的接收、处理、存储及数据库路径记录的过程。通过生成UUID作为文件名,确保了文件名的唯一性,同时实现了图片类型的检查和文件的压缩处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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;
        } 
        
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值