@RequestMapping(value = "doUpload", method = RequestMethod.POST)
public String doUpload(@ModelAttribute BookHelper bookHelper, Model model, HttpSession session) throws IllegalStateException, IOException, ParseException {
logger.info("you are uploading a book! ");
logger.info("This book is " + bookHelper.getTitle() + "!");
String fileName = bookHelper.getBookFile().getOriginalFilename();
String bookCover = bookHelper.getBookCover().getOriginalFilename();
MultipartFile bookFile = bookHelper.getBookFile();
MultipartFile coverFile = bookHelper.getBookCover();
if (bookFile.isEmpty()) {
logger.info("Uploading failed! The book you are uploading is empty!");
return "upload_failed";
} else if (coverFile.isEmpty()) {
logger.info("Uploading failed! The book cover you are uploading is empty!");
return "upload_failed";
} else {
String typeId = "" + bookHelper.getLargeType() + bookHelper.getSmallType();
int type_id = Integer.parseInt(typeId);
String format = fileName.substring(fileName.lastIndexOf('.') + 1);
List typeNames;
typeNames = bookService.getTypeNames(type_id);
String filePath_pre = (String) PropertyConfigurer.getProperty("book_path");
String filePath = filePath_pre + typeNames.get(0) +
"/" + typeNames.get(1) + "/" +
bookHelper.getTitle() + "." + format;
File localBookFile = new File(filePath);
if (localBookFile.exists()) {
logger.info("Uploading failed! The book is existed!");
return "upload_failed2";
}
bookFile.transferTo(localBookFile);
String coverPath_pre = (String) PropertyConfigurer.getProperty("book_cover_path");
String coverPath = coverPath_pre + typeNames.get(0) +
"/" + typeNames.get(1) + "/" +
bookHelper.getTitle() + ".jpg";
File localCoverFile = new File(coverPath);
coverFile.transferTo(localCoverFile);
logger.info("The book has uploaded to local path successfully!");
Book book = new Book();
book.setBook_title(bookHelper.getTitle());
book.setBook_author(bookHelper.getAuthor());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
Date date = dateFormat.parse(bookHelper.getPubYear());
book.setBook_pubYear(date);
book.setBook_summary(bookHelper.getSummary());
book.setType_id(type_id);
book.setBook_format(format);
book.setDownload_times(0);
book.setBook_file(filePath);
book.setBook_cover(coverPath);
dateFormat = new SimpleDateFormat("yyMMdd", Locale.CHINESE);
String pubDate = dateFormat.format(date);
String upDate = dateFormat.format(new Date());
int random = new Random().nextInt(900) + 100;
String idStr = "" + typeId + pubDate + upDate + random;
long bookID = Long.parseLong(idStr);
logger.info("The book id you uploaded is " + bookID);
book.setId(bookID);
bookService.uploadBook(book);
UserHelper userHelper = (UserHelper) session.getAttribute("userHelper");
bookService.updateRecords(userHelper.getId(), bookID);
userService.updateUserContribution(2, userHelper.getId());
model.addAttribute("bookID", bookID);
logger.info("you are coming to the uploading successful page!");
return "upload_success";
}
}
public List getTypeNames(int id) {
BookType bookType;
bookType = bookTypeDao.queryById(id);
List typeNames = new ArrayList();
typeNames.add(bookType.getLarge_type_name());
typeNames.add(bookType.getSmall_type_name());
return typeNames;
}
前端代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
敛书网 - 文件上传
<%@include file="common/loginHead.jsp"%>
<%@include file="common/userHead.jsp"%>
上传文件
http://localhost:8888/ebooknet_war_exploded/book_download?bookID=12211101211103496&filePath=E:/lianshu/ebooks/%E7%BB%8F%E5%85%B8%E6%96%87%E5%AD%A6/%E5%8F%A4%E5%85%B8%E6%96%87%E5%AD%A6/%E5%AD%9F%E5%AD%90.txt
@RequestMapping(value = "/book_download")
public void getBookDownload(long bookID, String filePath, HttpServletResponse response) {
response.setContentType("text/html;charset=utf-8");
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
long fileLength = new File(filePath).length();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(filePath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2018];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
bookService.addDownloadTimes(bookID);
logger.info("you are downloading the book, the book file is " + fileName);
}
}
@RequestMapping(value = "/infoModify")
public String infoModify(String name, String email, String avatarImg, HttpSession session) {
logger.info("The user is modifying his information!");
UserHelper userHelper = (UserHelper) session.getAttribute("userHelper");
User user = new User();
user.setId(userHelper.getId());
user.setUserName(name);
user.setEmail(email);
int avatarId = userService.getAvatarId(avatarImg);//根据头像存储地址获取头像的编号
user.setAvatarNum(avatarId);
userService.updateUserInfo(user);
User user1;
user1 = userService.queryById(userHelper.getId());//获取新的用户信息
UserHelper newUserHelper;
newUserHelper = userService.getLoginUser(user1.getUserCode(), user1.getUserPassword());
session.setAttribute("userHelper", newUserHelper);//重新存入Session
return "redirect:/person";
}