|
6 | 6 | import net.liuxuan.spring.Helper.SpringContextHelper;
|
7 | 7 | import org.json.JSONArray;
|
8 | 8 | import org.springframework.beans.factory.annotation.Autowired;
|
| 9 | +import org.springframework.beans.factory.annotation.Value; |
9 | 10 | import org.springframework.security.access.prepost.PreAuthorize;
|
10 | 11 | import org.springframework.stereotype.Controller;
|
| 12 | +import org.springframework.util.FileCopyUtils; |
11 | 13 | import org.springframework.web.bind.annotation.*;
|
12 | 14 | import org.springframework.web.method.HandlerMethod;
|
13 | 15 | import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
14 | 16 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
15 | 17 |
|
16 | 18 | import javax.annotation.PostConstruct;
|
| 19 | +import javax.servlet.ServletOutputStream; |
17 | 20 | import javax.servlet.http.HttpServletRequest;
|
18 | 21 | import javax.servlet.http.HttpServletResponse;
|
19 |
| -import java.io.File; |
20 |
| -import java.io.FileNotFoundException; |
| 22 | +import java.io.*; |
| 23 | +import java.net.URLConnection; |
| 24 | +import java.nio.charset.Charset; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
21 | 27 | import java.util.*;
|
22 | 28 | import java.util.stream.Collectors;
|
23 | 29 |
|
|
36 | 42 | @PreAuthorize("hasRole('ROLE_ADMIN')")
|
37 | 43 | public class CommonController {
|
38 | 44 |
|
39 |
| - |
| 45 | + @Value("${SprKi.upload.restrictedpath}") |
| 46 | + private String restrictedFilePath; |
40 | 47 | /**
|
41 | 48 | * The Request mapping handler mapping.
|
42 | 49 | */
|
@@ -87,6 +94,72 @@ public String getMsg(
|
87 | 94 | return path;
|
88 | 95 | }
|
89 | 96 |
|
| 97 | + @RequestMapping(value = "/moses", method = RequestMethod.GET) |
| 98 | + public String mosesParse(){ |
| 99 | + return "tools/parse"; |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + @RequestMapping(value = "/func/get", method = RequestMethod.GET) |
| 104 | + @ResponseBody |
| 105 | + public String getFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model){ |
| 106 | + String path = request.getParameter("file"); |
| 107 | + path = restrictedFilePath +'/'+path; |
| 108 | + File f = new File(path); |
| 109 | + String rtn; |
| 110 | + if(f.exists()&&(!f.isDirectory())){ |
| 111 | + rtn = path; |
| 112 | + }else{ |
| 113 | + rtn = path+ " : Not Existed!!!"; |
| 114 | + } |
| 115 | + |
| 116 | + return rtn; |
| 117 | + } |
| 118 | + @RequestMapping(value = "/func/get2", method = RequestMethod.GET) |
| 119 | +// @ResponseBody |
| 120 | + public void getFile2(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) throws IOException { |
| 121 | + String path = request.getParameter("file"); |
| 122 | + path = restrictedFilePath +'/'+path; |
| 123 | + File f = new File(path); |
| 124 | + String rtn; |
| 125 | + if(f.exists()&&(!f.isDirectory())){ |
| 126 | + rtn = path; |
| 127 | + }else{ |
| 128 | + String errorMessage = "Sorry. The file you are looking for does not exist"; |
| 129 | + System.out.println(errorMessage); |
| 130 | + ServletOutputStream outputStream = response.getOutputStream(); |
| 131 | + outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); |
| 132 | + outputStream.close(); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + String mimeType= URLConnection.guessContentTypeFromName(f.getName()); |
| 137 | + if(mimeType==null){ |
| 138 | + System.out.println("mimetype is not detectable, will take default"); |
| 139 | + mimeType = "application/octet-stream"; |
| 140 | + } |
| 141 | + |
| 142 | + System.out.println("mimetype : "+mimeType); |
| 143 | +// mimeType = "application/force-download"; |
| 144 | + response.setContentType(mimeType); |
| 145 | + /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser |
| 146 | + while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/ |
| 147 | +// response.setHeader("Content-Disposition", String.format("inline; filename=\"" + f.getName() +"\"")); |
| 148 | + response.setHeader("Content-Disposition", String.format("attachment; filename=\"" + f.getName() +"\"")); |
| 149 | + |
| 150 | + |
| 151 | + /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ |
| 152 | + //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); |
| 153 | + |
| 154 | + response.setContentLength((int)f.length()); |
| 155 | + |
| 156 | + InputStream inputStream = new BufferedInputStream(new FileInputStream(f)); |
| 157 | + |
| 158 | + //Copy bytes from source to destination(outputstream in this example), closes both streams. |
| 159 | + FileCopyUtils.copy(inputStream, response.getOutputStream()); |
| 160 | + |
| 161 | + } |
| 162 | + |
90 | 163 | /**
|
91 | 164 | * 为所有的用户加上头像
|
92 | 165 | *
|
|
0 commit comments