Collect from 企业网站模板

springboot上传文件工具

第一次登录送5个金币,绑定QQ送5个金币。金币可以用来观看视频和下载资源哦!

 package com.ssm.lab.untils;

  
 

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

 
import com.sun.DateUtils;

 

 
/**
 *  @author jf3q.com
 *
 */
public class UploadFile {

	 
     
     
     public static Map<String,String> upfile( MultipartFile file,HttpServletRequest request,String package1) {
    	 Map<String,String> m=new HashMap<String,String>();
    	 String fname=file.getOriginalFilename();
         m.put("name", fname);
    	 String format=fname.substring(fname.indexOf(".")+1, fname.length());
    	  
         String path = request.getSession().getServletContext().getRealPath(Constants.Upimgs.absolute_path+package1);  
         
         File pf=new File(path);
         if(!pf.exists()){  
        	 pf.mkdirs();  
         }
         
         String fileName = DateUtils.DateToString(new Date(), "yyyyMMddHHmmss")+UUID.randomUUID().toString().replace("-", "").substring(1, 11)+"."+format;  
           
         File targetFile = new File(path, fileName);  
         try {  
             file.transferTo(targetFile);
             
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
         m.put("path", Constants.Upimgs.absolute_path+package1+fileName);
        return     m;
     }

     public static void deleteFile(HttpServletRequest request,String path){   
		 	if(path.indexOf("faceimg")>-1)return;
			 String abPath= request.getSession().getServletContext().getRealPath("")+path;
			 abPath=abPath.replace("/", "\\");
			 File f=new File(abPath);
			 if(f.exists()){
				 System.out.println("del==="+abPath);
				 f.delete();
			 }
			  
    	
     } 
    
      
    
}

在此处输入评论

允许提问者通过qq联系我

    现在绑定QQ

点击加载更多...