Blame view

src/main/java/com/huaheng/pc/common/CommonController.java 4.4 KB
tangying authored
1
2
package com.huaheng.pc.common;
3
import com.google.zxing.WriterException;
mahuandong authored
4
import com.huaheng.common.config.Global;
5
import com.huaheng.common.config.ServerConfig;
pengyongcheng authored
6
import com.huaheng.common.utils.DateUtils;
7
import com.huaheng.common.utils.QRCodeGenerator;
8
import com.huaheng.common.utils.file.FileUploadUtils;
pengyongcheng authored
9
import com.huaheng.common.utils.file.FileUtils;
10
import com.huaheng.framework.config.HuaHengConfig;
11
import com.huaheng.framework.web.domain.AjaxResult;
tangying authored
12
13
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
14
15
16
17
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
tangying authored
18
import org.springframework.stereotype.Controller;
19
20
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
tangying authored
21
pengyongcheng authored
22
23
24
25
26
27
28
29
30
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
tangying authored
31
32
/**
 * 通用请求处理
pengyongcheng authored
33
 *
tangying authored
34
35
36
 * @author huaheng
 */
@Controller
pengyongcheng authored
37
public class CommonController {
tangying authored
38
39
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);
40
41
42
    @Resource
    private ServerConfig serverConfig;
tangying authored
43
    @RequestMapping("common/download")
44
45
    public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
        try {
pengyongcheng authored
46
47
            String dateTime = DateUtils.dateTimeNow();
            String realFileName = dateTime + fileName.substring(fileName.indexOf("_") + 1);
48
            String filePath = Global.getDownloadPath() + fileName;
tangying authored
49
50
51
52
53

            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=" + setFileDownloadHeader(request, realFileName));
            FileUtils.writeBytes(filePath, response.getOutputStream());
pengyongcheng authored
54
            if (Boolean.TRUE.equals(delete)) {
tangying authored
55
56
                FileUtils.deleteFile(filePath);
            }
pengyongcheng authored
57
        } catch (Exception e) {
tangying authored
58
59
60
61
            log.error("下载文件失败", e);
        }
    }
pengyongcheng authored
62
    public String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
tangying authored
63
64
        final String agent = request.getHeader("USER-AGENT");
        String filename = fileName;
pengyongcheng authored
65
        if (agent.contains("MSIE")) {
tangying authored
66
67
68
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
pengyongcheng authored
69
        } else if (agent.contains("Firefox")) {
tangying authored
70
71
            // 火狐浏览器
            filename = new String(fileName.getBytes(), "ISO8859-1");
pengyongcheng authored
72
        } else if (agent.contains("Chrome")) {
tangying authored
73
74
            // google浏览器
            filename = URLEncoder.encode(filename, "utf-8");
pengyongcheng authored
75
        } else {
tangying authored
76
77
78
79
80
81
            // 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;

    }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    @GetMapping(value = "/image/{text}")
    public ResponseEntity<byte[]> getImage(@PathVariable("text") String text) {
        byte[] qrcode = null;
        try {
            qrcode = QRCodeGenerator.getQRCodeImage(text, 100, 100);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Set headers
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(qrcode, headers, HttpStatus.CREATED);
    }
98
99
100
101
102
103

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    @ResponseBody
pengyongcheng authored
104
105
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
106
            // 上传文件路径
107
            String filePath = HuaHengConfig.getProfile();
108
109
110
111
112
113
114
115
116
117
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + "/" + fileName;
            AjaxResult ajax = AjaxResult.success();
            Map<String, Object> map = new HashMap<>();

            map.put("fileName", fileName);
            map.put("url", url);
            ajax.setData(map);
            return ajax;
pengyongcheng authored
118
        } catch (Exception e) {
119
120
121
            return AjaxResult.error(e.getMessage());
        }
    }
tangying authored
122
}