/
UploadController.java
117 lines (111 loc) · 4.36 KB
/
UploadController.java
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
package io.nutz.nutzsite.module.open.file;
import io.nutz.nutzsite.common.base.Globals;
import io.nutz.nutzsite.common.base.Result;
import io.nutz.nutzsite.common.utils.ObjectUtils;
import io.nutz.nutzsite.common.utils.UpLoadUtil;
import io.nutz.nutzsite.module.sys.services.ImageService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.impl.AdaptorErrorContext;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* @Author: Haimming
* @Date: 2019-05-08 13:42
* @Version 1.0
*/
@IocBean
@At("/open/file")
public class UploadController {
private static final Log log = Logs.get();
@Inject
private ImageService imageService;
@At("/get/?")
@Ok("raw")
public void get(String id,HttpServletRequest req, HttpServletResponse resp) {
String path = imageService.get(id);
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
try (InputStream fis = new BufferedInputStream(new FileInputStream(path))){
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
resp.reset();
// 设置response的Header
resp.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
resp.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(resp.getOutputStream());
resp.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@At
@Ok("json")
@POST
@AdaptBy(type = UploadAdaptor.class)
public Object upload(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
try {
if (err != null && err.getAdaptorErr() != null) {
return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
} else if (tf == null) {
return Result.error("空文件");
} else {
String url = UpLoadUtil.upLoadFileSysConfigPath(tf,"tmp");
String u = req.getServletContext().getContextPath();
return Result.success("上传成功", u + url );
}
} catch (Exception e) {
e.printStackTrace();
return Result.error("系统错误");
} catch (Throwable e) {
return Result.error("图片格式错误");
}
}
@At("/userfiles/?")
@GET
@POST
@Ok("raw")
public void fileOutputStream(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String filepath = req.getRequestURI();
int index = filepath.indexOf(Globals.USERFILES_BASE_URL);
if(index >= 0) {
filepath = filepath.substring(index + Globals.USERFILES_BASE_URL.length());
}
try {
filepath = URLDecoder.decode(filepath, Globals.UTF8);
} catch (UnsupportedEncodingException e1) {
log.error(String.format("解释文件路径失败,URL地址为%s", filepath), e1);
}
File file = new File(Globals.getUserfilesBaseDir() + Globals.USERFILES_BASE_URL + filepath);
try {
ObjectUtils.copy(new FileInputStream(file), resp.getOutputStream());
resp.setHeader("Content-Type", "application/octet-stream");
return;
} catch (FileNotFoundException e) {
req.setAttribute("exception", new FileNotFoundException("请求的文件不存在"));
req.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(req, resp);
}
}
}