eeee,因为一些原因我这里只有后台。
主要的操作只有一些基础的增删改查,然后就是上传下载还有就是删除文件。
路径我是写在了yml中然后通过@ConfigurationProperties注解建立一个路径的properties的映射叫filePath,之后再使用路径。
api的接口是用swagger写的,但是我这里用的方法不对,不过swagger还是很好用的不过他要到几个依赖1
2
3
4
5
6
7
8
9
10<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
还有就是这两个依赖的版本最好一样,不然会出现bug,接口文档通过注解,然后再通过浏览器获取网址
网址一般都是:http://localhost:8080/swagger-ui.html
然后还可以在上面测试接口。
我这回用的是前后端分离,通过controller返回的是一个Result的模板类,其中有前端向后台请求的数据和返回的关于请求的信息。
上传的主要逻辑是先获取上传文件,然后判断是否是压缩文件,不是的话就返回用户他的文件信息,反之继续。文件的路径名用filePath中的路径当做母路径,母路径上再加上他的文件id、时间、一个随机数作为他的子路径。然后再用file.transferTo进行上传。之后再更新他的任务的信息,成员的信息,和提交信息。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
90package com.ctgu.upndo.controller;
import com.ctgu.upndo.pojo.Member;
import com.ctgu.upndo.pojo.Submit;
import com.ctgu.upndo.pojo.Task;
import com.ctgu.upndo.propeties.propeties;
import com.ctgu.upndo.service.SaveAndFlushService;
import com.ctgu.upndo.service.SearchService;
import com.ctgu.upndo.utils.CodeEnum;
import com.ctgu.upndo.utils.GetWayRan;
import com.ctgu.upndo.utils.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.awt.print.Pageable;
import java.io.File;
@RestController
@RequestMapping("/upload")
public class UploadController {
@Autowired
propeties filepath;
@Autowired
SearchService searchService;
@Autowired
SaveAndFlushService saveAndFlushService;
@ApiOperation(value="上传")
@ApiImplicitParams({
@ApiImplicitParam(name="file",value="前端文件,只有一个"),
@ApiImplicitParam(name="submit",value = "提交详细Submit对象")
})
@RequestMapping("/one")
public Result<Integer> upload( @RequestParam("file") MultipartFile file, @RequestParam("submit") Submit submit) {
Result<Integer> res = new Result<>();
try {
if (file.isEmpty()) {
res.setResult("文件是空的啊..");
return res;
}
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
if (suffixName != "zip" && suffixName!="rar") {
res.setResult("30008","文件必须是zip和rar的啊,你的怎么是" + suffixName + "的啊");
return res;
}
Result<Member> rm = searchService.findMemberByTidAndUid(submit.getTid(), submit.getSid());
if(rm.getData()==null){
res.setResult(CodeEnum.UPLOAD_ERROR);
return res;
}
Member member=rm.getData();
Integer times = rm.getData().getTimes();
times++;
Integer ta = submit.getTid();
member.setTimes(times);
String ran= GetWayRan.getUpLoadRandom();
String path = filepath.getUpload_path()+ta.toString()+File.separator+ran+suffixName;
submit.setWay(path);
member.setLast(path);
File fi = new File(path);
if (!fi.getParentFile().exists()) {
fi.getParentFile().mkdir();
}
if (times == 1) {
Result<Task> rt = searchService.findTaskByTid(submit.getTid());
Task task=rt.getData();
Integer number = task.getFinish();
number++;
task.setFinish(number);
saveAndFlushService.SaveAndFlushTask(task);
}
file.transferTo(fi);
saveAndFlushService.SaveSubmit(submit);
saveAndFlushService.SaveAndFlushMember(member);
} catch (Exception e) {
res.setResult(CodeEnum.UPLOAD_ERROR);
}
res.setResult(CodeEnum.UPDATE_SUCCESS);
return res;
}
}
然后是下载,下载分为 两种学生下载,和管理员下载。学生下载需要前端传来判断信息。然后通过路径通过缓冲流进行下载。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
66package com.ctgu.upndo.controller;
import com.ctgu.upndo.pojo.Submit;
import com.ctgu.upndo.service.SearchService;
import com.ctgu.upndo.utils.CodeEnum;
import com.ctgu.upndo.utils.GetNumber;
import com.ctgu.upndo.utils.Result;
import com.ctgu.upndo.utils.ToNumber;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
@RestController
@RequestMapping("/download")
public class DownloadController {
@Autowired
SearchService searchService;
@ApiOperation(value="下载")
@ApiImplicitParam(name="list",value="String对象,下载项目序号")
@RequestMapping(value = "student")
public Result<Integer> upload(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "path") String path,@RequestParam(value = "judge") String judge){
Result<Integer> res=new Result<>();
Integer flag= ToNumber.getNumber(judge);
if(flag==0){
res.setResult("30013","未在时限内,不可下载");
}
res.setResult(CodeEnum.DOWNLOAD_SUCCESS);
String status=null;
String fileName=new String();
BufferedInputStream bis = null;
try{
File file=new File(path);
if(!file.exists()){
res.setResult("30012","文件为空");
return res;
}
fileName=file.getName();
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
byte[] buffer=new byte[1024];
bis=new BufferedInputStream(new FileInputStream(file));
OutputStream os=response.getOutputStream();
int number=bis.read(buffer);
while(number!=-1){
os.write(buffer,0,number);
number=bis.read(buffer);
}
}catch(Exception e){
res.setResult(CodeEnum.DOWNLOAD_ERROR);
}
return res;
}
}
管理员的复杂一点,前端传来字符串然后我进行处理得出他的需要下载的id。然后将文件打包成zip放在客户端上进行下载。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
105package com.ctgu.upndo.controller;
import com.ctgu.upndo.mapper.MemberMapper;
import com.ctgu.upndo.mapper.TaskMapper;
import com.ctgu.upndo.pojo.Member;
import com.ctgu.upndo.pojo.Task;
import com.ctgu.upndo.utils.CodeEnum;
import com.ctgu.upndo.utils.GetNumber;
import com.ctgu.upndo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@RequestMapping(value = "/downloads")
public class Download {
@Autowired
MemberMapper memberMapper;
@Autowired
TaskMapper taskMapper;
public Result<Integer> downloads(HttpServletResponse res, String list) throws IOException {
Result<Integer> result = new Result<>();
List<Integer> numbers = GetNumber.getnumber(list);
int len = numbers.size();
List<String> filePaths = new ArrayList<>();
Member member = new Member();
//String way = new String();
for (int i = 0; i < len; i++) {
member = memberMapper.findMemberById(numbers.get(i));
filePaths.add(member.getLast());
}
Task task = taskMapper.findTaskById(member.getTid());
String name = task.getTitle();
result.setResult(CodeEnum.SCCESS);
try {
File file = new File(member.getLast());
String zipBasePath = file.getAbsolutePath();
String zipName = name + ".zip";
String zipFilePath = zipBasePath + File.separator + zipName;
res.setCharacterEncoding("UTF-8"); //设置编码字符
res.setContentType("application/force-download;charset=UTF-8"); //设置下载内容类型
OutputStream out = res.getOutputStream(); //创建页面返回方式为输出流,会自动弹出下载框
File zip = new File(zipFilePath);
if (!zip.exists()) {
zip.createNewFile();
}
//创建zip文件输出流
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
this.zipFile(zipBasePath, zipName, zipFilePath, filePaths, zos);
zos.close();
res.setHeader("Content-disposition", "attachment;filename=" + zipName);//设置下载的压缩文件名称
//将打包后的文件写到客户端,输出的方法同上,使用缓冲流输出
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFilePath));
byte[] buff = new byte[bis.available()];
bis.read(buff);
bis.close();
out.write(buff);//输出数据文件
out.flush();//释放缓存
out.close();//关闭输出流
} catch (Exception e) {
result.setResult(CodeEnum.DOWNLOAD_ERROR);
}
return result;
}
private String zipFile(String zipBasePath, String zipName, String zipFilePath, List<String> filePaths, ZipOutputStream zos) throws IOException {
//循环读取文件路径集合,获取每一个文件的路径
for (String filePath : filePaths) {
File inputFile = new File(filePath); //根据文件路径创建文件
if (inputFile.exists()) { //判断文件是否存在
//创建输入流读取文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));
//将文件写入zip内,即将文件进行打包
zos.putNextEntry(new ZipEntry(inputFile.getName()));
//写入文件的方法
int size = 0;
byte[] buffer = new byte[1024]; //设置读取数据缓存大小
while ((size = bis.read(buffer)) > 0) {
zos.write(buffer, 0, size);
}
//关闭输入输出流
zos.closeEntry();
bis.close();
}
}
return null;
}
}
最后就是我的工具类,里面都是我对字符串的处理的一些类和我的Result类和CodeEnum。
最后就是觉得自己对jpa了解了更多了吧。然后就是吸取了很多教训。虽然做的很不好不过感觉收获还是蛮大的。