liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package framework.fileUpload;
 
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import framework.base.FrameBaseController;
import framework.startup.FrameUtil;
 
/**
 * 通用的文件上传类,与sys.js中的 $.commonFileUpload一起使用<br/>
 * 
 * 需要验证权限,一般只能登录才可上传
 * 
 * @author liuyajun, 8384503@qq.com
 * @date 2016年1月18日
 * @time 下午7:18:41
 */
@Controller
public class FileUploadController extends FrameBaseController {
 
    @Autowired
    MyMultipartResolver resolver;
    
    @RequestMapping(value = "systemFileUpload", method = RequestMethod.POST)
    public String upload(HttpServletRequest request, 
            @Param("getProgress") String getProgress,
            @Param("clear") String clear) throws Exception {
 
        //清空session,以备记录上传进度
        if(clear !=null && "y".equals(clear)){
            HttpSession session = request.getSession();
            session.removeAttribute(FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY_MAX);
            session.removeAttribute(FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY);
            return this.ajax(null);
        }
        
        //获取上传进度
        if(getProgress !=null && "y".equals(getProgress)){
            HttpSession session = request.getSession();
            
            String max =(String)session.getAttribute(
                    FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY_MAX);
            String percent = (String)session.getAttribute(
                    FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY);
            
            if(max !=null){
                return this.ajax(max);
            }
            return this.ajax(percent);
        }
        
        //接收上传的文件
        resolver.setServletContext(request.getSession().getServletContext());
 
        String uploadPath = FrameUtil.getUploadFilePath();
        
        if (resolver.isMultipart(request)) {
 
            MultipartHttpServletRequest req = (MultipartHttpServletRequest)request; 
            
            Iterator<String> iter = req.getFileNames();
            
            String split = FileUploadHelper.SPLIT;
            StringBuffer ss = new StringBuffer();
            
            while (iter.hasNext()) {
                
                String inputName = iter.next();
                
                List<MultipartFile> fileList = req.getFiles(inputName);
                
                for(MultipartFile file : fileList){
                    if(file==null || file.getOriginalFilename().trim().length()==0){
                        continue;
                    }
                    
//                    CommonsMultipartFile cf= (CommonsMultipartFile)file; 
//                    DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
//                    File f = fi.getStoreLocation();
//                    System.err.println(f.getAbsolutePath());
                    
                    String oriName = file.getOriginalFilename().trim();
 
                    String newName = UUID.randomUUID().toString().toLowerCase()
                            .replace("-", "").replace("/", "");
                    String fileString = inputName+split+newName+split+oriName;
                    
                    ss.append(split).append(split).append(fileString);
                    
                    String newPath = uploadPath + newName;
                    
                    file.transferTo(new File(newPath));
                }
            }
            
            if(ss.length()>0){
                ss.delete(0, 2);
            }
            
            /*
             * 上传文件控件name/保存的文件名(相对于上传目录)/文件原始名//
             */
            this.getRequest().setAttribute("fileInfo", ss.toString());
        }
 
        return "forward:/frame-jsp/file-upload-succuess.jsp";
    }
}