cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.web.controller.business.component;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
import cn.ksource.beans.GG_FILES;
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.DownloadUtil;
import cn.ksource.core.util.FtpClient;
import cn.ksource.core.util.JsonUtil;
import cn.ksource.core.util.UploadStatus;
import cn.ksource.core.web.WebUtil;
import cn.ksource.web.service.file.FileService;
 
 
@SuppressWarnings("rawtypes")
@Controller
@RequestMapping("/platform/fileUpload")
public class FileUploadController {
    
    private FileService fileService;
    /**
     * 文件上传
     * 
     * @author chenlong
     * @return
     */
    @RequestMapping(value = "/uploadIndex", method = RequestMethod.GET)
    public String uploadFile() {
        return "fileUpload/uploadIndex";
    }
    
    @RequestMapping(value = "/Upload2Ftp.html", method = RequestMethod.POST)
    public void doUploadFile(@RequestParam(value="file") CommonsMultipartFile file, HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException {
        Map<String,String> res = new HashMap<String,String>(); 
        if (file != null) {
            String fileName = file.getOriginalFilename(); 
            long fileSize = file.getSize(); 
            String path = "/cl/doc/";
            String remote = path + fileName;
            String ext = fileName.substring(fileName.lastIndexOf(".") + 1).trim().toLowerCase();
              
            InputStream is = file.getInputStream();
            FtpClient ftpClient = new FtpClient(); 
            ftpClient.connect();
            UploadStatus status = ftpClient.upload(is, remote);
            System.out.println("-----------"+status);
            ftpClient.disconnect();
            //上传到图片服务器
            //file.transferTo(new File("e:/"+fileName));
            
            res.put("fileName", fileName);
            res.put("path", remote);
            res.put("fileSize", ConvertUtil.obj2StrBlank(fileSize));
            res.put("ext", ext);
        }
        WebUtil.write(response, JsonUtil.map2Json(res));
    }
    
    @RequestMapping(value = "/downFtpFile.html")
    public void downFtpFile(HttpServletRequest request,HttpServletResponse response){
        String fileId = request.getParameter("fileId");
        GG_FILES file = new GG_FILES(fileId).getInstanceById();
        String remote = file.getFile_path();
        FtpClient ftpClient = new FtpClient(); 
        try {
            ftpClient.connect();
            InputStream in = ftpClient.download(remote);
            ftpClient.disconnect();
            DownloadUtil.download(response, in, file.getFile_name() );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}