Skip to content

Commit 6bd97ad

Browse files
committed
update method of upload and download
1 parent 50bc76f commit 6bd97ad

File tree

4 files changed

+137
-51
lines changed

4 files changed

+137
-51
lines changed

src/com/snnu/edu/controller/AuthorController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public String paperAddAuthor(Integer paper_id, Authors author) {
4646
@ResponseBody
4747
//通过文章查询作者
4848
public String getPaperAuthor(Integer paper_id) {
49-
List<Authors> list = authorservice.getAuthorByPaperId(paper_id);
49+
List<Authors> authors = authorservice.getAuthorByPaperId(paper_id);
5050
Map map = new HashMap();
51-
if(list!=null) {
52-
map.put("authors", list);
51+
if(authors.size() >= 0) {
52+
map.put("authors", authors);
5353
map.put("code", 200);
5454
map.put("msg", "OK");
5555
}else {
@@ -63,10 +63,10 @@ public String getPaperAuthor(Integer paper_id) {
6363
@ResponseBody
6464
public String getAllAuthor() {
6565
AuthorService authorservice = new AuthorServiceImpl();
66-
List<Authors> list = authorservice.getAllAuthor();
66+
List<Authors> authors = authorservice.getAllAuthor();
6767
Map map = new HashMap();
68-
if(list!=null) {
69-
map.put("authors", list);
68+
if(authors.size() >= 0) {
69+
map.put("authors", authors);
7070
map.put("code", 200);
7171
map.put("msg", "OK");
7272
}else {

src/com/snnu/edu/controller/PaperController.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.snnu.edu.controller;
22

3-
import java.io.File;
43
import java.util.ArrayList;
54
import java.util.Date;
65
import java.util.HashMap;
@@ -13,7 +12,7 @@
1312
import org.springframework.web.bind.annotation.RequestMapping;
1413
import org.springframework.web.bind.annotation.ResponseBody;
1514

16-
import com.google.gson.Gson;
15+
import com.snnu.edu.entity.Authors;
1716
import com.snnu.edu.entity.Papers;
1817
import com.snnu.edu.entity.Users;
1918
import com.snnu.edu.serviceInterface.PaperService;
@@ -31,13 +30,7 @@ public class PaperController {
3130

3231
@RequestMapping("add")
3332
@ResponseBody
34-
public String add(Papers paper,String title, HttpSession session) {
35-
System.out.println("要保存的文章信息:======"+title);
36-
System.out.println("要保存的文章信息:"+paper.getTitle()+"======"+title);
37-
Users user = (Users) session.getAttribute("current_user");
38-
if(user != null){
39-
paper.setUser(user);
40-
}
33+
public String add(Papers paper) {
4134
String paper_number = Tools.getPaper_number();
4235
Date submit_time = Tools.getTime();
4336
paper.setpaper_number(paper_number);
@@ -92,8 +85,8 @@ public String getCurrentUserPaper(Integer userId, HttpSession session) {
9285
papers = paperservice.getPaperByUserId(userId);
9386
} else {
9487
Users user = (Users) session.getAttribute("current_user");
95-
System.out.println("session中用户:" + user.getId());
96-
papers = paperservice.getPaperByUserId(user.getId());
88+
// System.out.println("session中用户:" + user.getId());
89+
// papers = paperservice.getPaperByUserId(user.getId());
9790
}
9891
System.out.println("list论文集合:" + papers);
9992
Map map = new HashMap();
@@ -125,7 +118,7 @@ public String getAllPaper() {
125118
}
126119
return Tools.getJson(map);
127120
}
128-
121+
//更改文章的状态
129122
@RequestMapping("update_paper_status")
130123
@ResponseBody
131124
public String updatePaper(Integer paperId, Integer status) {

src/com/snnu/edu/controller/ToolsController.java

Lines changed: 124 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.snnu.edu.controller;
22

3+
import java.io.File;
4+
import java.io.FileInputStream;
35
import java.io.FileOutputStream;
46
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.net.URLEncoder;
9+
import java.util.ArrayList;
510
import java.util.Date;
611
import java.util.HashMap;
12+
import java.util.List;
713

14+
import javax.servlet.http.HttpServletResponse;
815
import javax.servlet.http.HttpSession;
916

1017
import org.springframework.stereotype.Controller;
@@ -20,64 +27,149 @@
2027
@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
2128
public class ToolsController {
2229

23-
@RequestMapping("upload")
30+
@RequestMapping("upload-single")
2431
@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()) {
2836
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/");
3141
FileOutputStream os = new FileOutputStream(realPath + filename);
3242
InputStream in = file.getInputStream();
3343
int b = 0;
34-
while((b=in.read()) != -1){
44+
while ((b = in.read()) != -1) {
3545
os.write(b);
36-
}
46+
}
3747
os.flush();
38-
os.close();
39-
in.close();
48+
os.close();
49+
in.close();
4050
} catch (Exception e) {
4151
// TODO Auto-generated catch block
4252
e.printStackTrace();
4353
}
4454
}
4555
}
4656

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+
47103
@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();
64150
}
151+
map.put("content", "");
152+
map.put("code", 200);
153+
map.put("msg", "OK");
154+
return Tools.getJson(map);
65155
}
156+
66157
@RequestMapping("sendEmail")
67158
@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 {
69161
boolean flag = Tools.sendEmail(from, to, content);
70162
HashMap map = new HashMap();
71-
if(flag){
163+
if (flag) {
72164
map.put("content", "");
73-
map.put("code",200);
165+
map.put("code", 200);
74166
map.put("msg", "OK");
75-
}else{
167+
} else {
76168
map.put("content", "");
77-
map.put("code",500);
169+
map.put("code", 500);
78170
map.put("msg", "server error");
79171
}
80-
172+
81173
return Tools.getJson(map);
82174
}
83175

src/com/snnu/edu/controller/UserController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ public String findStatus(HttpSession session) throws Exception {
7474
if (user != null) {
7575
user.setPassword("");
7676
hashmap.put("loginStatus", true);
77-
hashmap.put("user", user);
77+
hashmap.put("content", user);
7878
hashmap.put("code", 200);
7979
hashmap.put("msg", "OK");
8080
} else {
8181
hashmap.put("loginStatus", false);
82+
hashmap.put("content", "");
8283
hashmap.put("code", 200);
8384
hashmap.put("msg", "not login");
8485
}

0 commit comments

Comments
 (0)