本周已经来到了实训的末尾,学生端后端主要任务是对毕业端进行完善,对一些小问题进行优化。
1)毕业论文上传功能:
接口:
http://localhost:8081/student/uploadStudentGraduatePaper
实现:
/**
* 上传毕业论文,并保存到./static/paper
*
* @param request
* @param
* @return
* @throws ParseException
**/
@RequestMapping(value = "/uploadStudentGraduatePaper")
@ResponseBody
@Transactional(rollbackFor = {RuntimeException.class, Error.class})
public Result uploadStudentGraduatePaper(HttpServletRequest request, MultipartFile file) throws ParseException, IOException {
int stu_id = Integer.parseInt(request.getParameter("stu_id"));
String stu_Id=Integer.toString(stu_id);
byte[] paper = new byte[0];
if (file != null) {
paper=savePaper(file.getInputStream(), stu_Id , 1);
}
if (paper.length == 0) {
return Result.fail("更新失败");
} else {
return Result.succ("上传成功");
}
}
private byte[] savePaper(InputStream inputStream, String fileName,int i) {
byte[] barray = null;
OutputStream os = null;
try {
String path = null;
if(i==1) {//毕业论文
path = "./static/paper/studentGraduatePaper";
}
else{//外审论文
path = "./static/paper/studentEReviewPaper";
}
byte[] bs = new byte[1024 * 1024 * 128];
int len;
File tempFile = new File(path);
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName+".pdf");
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
File tempFile;
if(i==1) {
tempFile = new File("./static/paper/studentGraduatePaper/" + fileName+".pdf");
}
else{
tempFile = new File("./static/paper/studentEReviewPaper/" + fileName+".pdf");
}
barray = fileConvertToByteArray(tempFile);
return barray;
}
postman测试:
学号为201656788的学生上传了自己的毕业论文
后端接收论文并以学号重名名保存到了指定的毕业论文存储文件夹中。
2)外审论文上传功能:
接口:
http://localhost:8081/student/uploadStudentEReviewPaper
实现:
/**
* 上传毕业论文,并保存到./static/paper
*
* @param request
* @param
* @return
* @throws ParseException
**/
@RequestMapping(value = "/uploadStudentEReviewPaper")
@ResponseBody
@Transactional(rollbackFor = {RuntimeException.class, Error.class})
public Result uploadStudentEReviewPaper(HttpServletRequest request, MultipartFile file) throws ParseException, IOException {
int stu_id = Integer.parseInt(request.getParameter("stu_id"));
String stu_Id=Integer.toString(stu_id);
byte[] paper = new byte[0];
if (file != null) {
paper=savePaper(file.getInputStream(), stu_Id , 2);
}
/*int i = StuService.uploadPaper(paper);
if (i == 0) {
return Result.fail("更新失败");
} else {
return Result.succ("更新成功");
}*/
if (paper.length == 0) {
return Result.fail("更新失败");
} else {
return Result.succ("上传成功");
}
}
private byte[] savePaper(InputStream inputStream, String fileName,int i) {
byte[] barray = null;
OutputStream os = null;
try {
String path = null;
if(i==1) {//毕业论文
path = "./static/paper/studentGraduatePaper";
}
else{//外审论文
path = "./static/paper/studentEReviewPaper";
}
byte[] bs = new byte[1024 * 1024 * 128];
int len;
File tempFile = new File(path);
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName+".pdf");
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
File tempFile;
if(i==1) {
tempFile = new File("./static/paper/studentGraduatePaper/" + fileName+".pdf");
}
else{
tempFile = new File("./static/paper/studentEReviewPaper/" + fileName+".pdf");
}
barray = fileConvertToByteArray(tempFile);
return barray;
}
postman测试:
学号为201656788的学生上传自己的外审论文的存储文件夹。
在该文件夹中可以查看到以学号重命名的论文文件,证明上传成功。