|
1 | 1 | package com.snnu.edu.controller; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
3 | 5 | import java.io.FileOutputStream; |
4 | 6 | import java.io.InputStream; |
| 7 | +import java.io.OutputStream; |
| 8 | +import java.net.URLEncoder; |
| 9 | +import java.util.ArrayList; |
5 | 10 | import java.util.Date; |
6 | 11 | import java.util.HashMap; |
| 12 | +import java.util.List; |
7 | 13 |
|
| 14 | +import javax.servlet.http.HttpServletResponse; |
8 | 15 | import javax.servlet.http.HttpSession; |
9 | 16 |
|
10 | 17 | import org.springframework.stereotype.Controller; |
|
20 | 27 | @SuppressWarnings({ "unchecked", "unused", "rawtypes" }) |
21 | 28 | public class ToolsController { |
22 | 29 |
|
23 | | - @RequestMapping("upload") |
| 30 | + @RequestMapping("upload-single") |
24 | 31 | @ResponseBody |
25 | | - public void upload(@RequestParam("file") CommonsMultipartFile file,HttpSession session)throws Exception { |
26 | | - System.out.println("filename------->"+file.getOriginalFilename()); |
27 | | - if(!file.isEmpty()){ |
| 32 | + public void upload(@RequestParam("file") CommonsMultipartFile file, |
| 33 | + HttpSession session) throws Exception { |
| 34 | + System.out.println("filename------->" + file.getOriginalFilename()); |
| 35 | + if (!file.isEmpty()) { |
28 | 36 | try { |
29 | | - String filename = new Date().getTime()+file.getOriginalFilename(); |
30 | | - String realPath = session.getServletContext().getRealPath("/WEB-INF/upload/"); |
| 37 | + String filename = new Date().getTime() |
| 38 | + + file.getOriginalFilename(); |
| 39 | + String realPath = session.getServletContext().getRealPath( |
| 40 | + "/WEB-INF/upload/"); |
31 | 41 | FileOutputStream os = new FileOutputStream(realPath + filename); |
32 | 42 | InputStream in = file.getInputStream(); |
33 | 43 | int b = 0; |
34 | | - while((b=in.read()) != -1){ |
| 44 | + while ((b = in.read()) != -1) { |
35 | 45 | os.write(b); |
36 | | - } |
| 46 | + } |
37 | 47 | os.flush(); |
38 | | - os.close(); |
39 | | - in.close(); |
| 48 | + os.close(); |
| 49 | + in.close(); |
40 | 50 | } catch (Exception e) { |
41 | 51 | // TODO Auto-generated catch block |
42 | 52 | e.printStackTrace(); |
43 | 53 | } |
44 | 54 | } |
45 | 55 | } |
46 | 56 |
|
| 57 | + @RequestMapping("upload") |
| 58 | + public String threeFileUpload( |
| 59 | + @RequestParam("file") CommonsMultipartFile files[], |
| 60 | + HttpSession session) { |
| 61 | + |
| 62 | + List<String> list = new ArrayList<String>(); |
| 63 | + // 获得项目的路径 |
| 64 | + String path = session.getServletContext().getRealPath( |
| 65 | + "/WEB-INF/upload/"); |
| 66 | + // 上传位置 |
| 67 | + File f = new File(path); |
| 68 | + if (!f.exists()) |
| 69 | + f.mkdirs(); |
| 70 | + for (int i = 0; i < files.length; i++) { |
| 71 | + // 获得原始文件名 |
| 72 | + String fileName = files[i].getOriginalFilename(); |
| 73 | + System.out.println("原始文件名:" + fileName); |
| 74 | + // 新文件名 |
| 75 | + String newFileName = new Date().getTime() |
| 76 | + + files[i].getOriginalFilename(); |
| 77 | + if (!files[i].isEmpty()) { |
| 78 | + try { |
| 79 | + FileOutputStream fos = new FileOutputStream(path |
| 80 | + + newFileName); |
| 81 | + InputStream in = files[i].getInputStream(); |
| 82 | + int b = 0; |
| 83 | + while ((b = in.read()) != -1) { |
| 84 | + fos.write(b); |
| 85 | + } |
| 86 | + fos.close(); |
| 87 | + in.close(); |
| 88 | + } catch (Exception e) { |
| 89 | + e.printStackTrace(); |
| 90 | + } |
| 91 | + } |
| 92 | + System.out.println("上传文件到:" + path + newFileName); |
| 93 | + list.add(path + newFileName); |
| 94 | + |
| 95 | + } |
| 96 | + HashMap map = new HashMap(); |
| 97 | + map.put("content", list); |
| 98 | + map.put("code", 200); |
| 99 | + map.put("msg", "OK"); |
| 100 | + return Tools.getJson(map); |
| 101 | + } |
| 102 | + |
47 | 103 | @RequestMapping("download") |
48 | | - @ResponseBody |
49 | | - public void download(@RequestParam("file") CommonsMultipartFile file)throws Exception { |
50 | | - System.out.println("filename------->"+file.getOriginalFilename()); |
51 | | - if(!file.isEmpty()){ |
52 | | - FileOutputStream fos = new FileOutputStream(file.getOriginalFilename()); |
53 | | - byte[] buffer = new byte[1024 * 1024]; |
54 | | - int bytesum = 0; |
55 | | - int byteread = 0; |
56 | | - InputStream in = file.getInputStream(); |
57 | | - while ((byteread = in.read(buffer)) != -1) { |
58 | | - bytesum += byteread; |
59 | | - fos.write(buffer, 0, byteread); |
60 | | - fos.flush(); |
61 | | - } |
62 | | - fos.close(); |
63 | | - in.close(); |
| 104 | + public String downFile(String fileName, HttpSession session, |
| 105 | + HttpServletResponse response) { |
| 106 | + HashMap map = new HashMap(); |
| 107 | + System.out.println("1"); |
| 108 | + try { |
| 109 | + fileName = new String(fileName.getBytes("iso8859-1"), "UTF-8"); |
| 110 | + System.out.println("3"); |
| 111 | + // 获取上传文件的目录 |
| 112 | + String fileSaveRootPath = session.getServletContext().getRealPath( |
| 113 | + "/WEB-INF/upload"); |
| 114 | + System.out.println("4"); |
| 115 | + System.out.println(fileSaveRootPath + "\\" + fileName); |
| 116 | + // 得到要下载的文件 |
| 117 | + File file = new File(fileSaveRootPath + "\\" + fileName); |
| 118 | + |
| 119 | + // 如果文件不存在 |
| 120 | + if (!file.exists()) { |
| 121 | + map.put("content", ""); |
| 122 | + map.put("code", 400); |
| 123 | + map.put("msg", "您要下载的资源已被删除!!"); |
| 124 | + System.out.println("您要下载的资源已被删除!!"); |
| 125 | + } |
| 126 | + // 处理文件名 |
| 127 | + String realname = fileName.substring(fileName.indexOf("_") + 1); |
| 128 | + // 设置响应头,控制浏览器下载该文件 |
| 129 | + response.setHeader("content-disposition", "attachment;filename=" |
| 130 | + + URLEncoder.encode(realname, "UTF-8")); |
| 131 | + // 读取要下载的文件,保存到文件输入流 |
| 132 | + FileInputStream in = new FileInputStream(fileSaveRootPath + "\\" |
| 133 | + + fileName); |
| 134 | + // 创建输出流 |
| 135 | + OutputStream out = response.getOutputStream(); |
| 136 | + // 创建缓冲区 |
| 137 | + byte buffer[] = new byte[1024]; |
| 138 | + int len = 0; |
| 139 | + // 循环将输入流中的内容读取到缓冲区当中 |
| 140 | + while ((len = in.read(buffer)) > 0) { |
| 141 | + // 输出缓冲区的内容到浏览器,实现文件下载 |
| 142 | + out.write(buffer, 0, len); |
| 143 | + } |
| 144 | + // 关闭文件输入流 |
| 145 | + in.close(); |
| 146 | + // 关闭输出流 |
| 147 | + out.close(); |
| 148 | + } catch (Exception e) { |
| 149 | + e.printStackTrace(); |
64 | 150 | } |
| 151 | + map.put("content", ""); |
| 152 | + map.put("code", 200); |
| 153 | + map.put("msg", "OK"); |
| 154 | + return Tools.getJson(map); |
65 | 155 | } |
| 156 | + |
66 | 157 | @RequestMapping("sendEmail") |
67 | 158 | @ResponseBody |
68 | | - public String sendEmail(String from,String to,String content)throws Exception { |
| 159 | + public String sendEmail(String from, String to, String content) |
| 160 | + throws Exception { |
69 | 161 | boolean flag = Tools.sendEmail(from, to, content); |
70 | 162 | HashMap map = new HashMap(); |
71 | | - if(flag){ |
| 163 | + if (flag) { |
72 | 164 | map.put("content", ""); |
73 | | - map.put("code",200); |
| 165 | + map.put("code", 200); |
74 | 166 | map.put("msg", "OK"); |
75 | | - }else{ |
| 167 | + } else { |
76 | 168 | map.put("content", ""); |
77 | | - map.put("code",500); |
| 169 | + map.put("code", 500); |
78 | 170 | map.put("msg", "server error"); |
79 | 171 | } |
80 | | - |
| 172 | + |
81 | 173 | return Tools.getJson(map); |
82 | 174 | } |
83 | 175 |
|
|
0 commit comments