spring mvc 上传文件

来源:互联网 发布:apt模型 知乎 编辑:程序博客网 时间:2024/05/22 11:55

@RequestMapping("/bid/post")
public String bidList(ProjectPost projectPost, Model model,
@RequestParam CommonsMultipartFile bidFile,
HttpServletRequest request) throws Exception {
Member sessionUser = getSessionUser(request);
long enterpriseId = sessionUser.getEnterprise().getId();
long projectId = sessionUser.getProjectProfileId();
projectPost.setEnterpriseId(enterpriseId);
ProjectDoc projectDoc = new ProjectDoc();

if (bidFile != null) {
if (!bidFile.getOriginalFilename().equals("")) {
// String uploadPath =
// request.getServletContext().getRealPath("")+ File.separator +
// UPLOAD_DIRECTORY;
String uploadPath = request.getContextPath() + File.separator
+ UPLOAD_DIRECTORY;

String filePath = uploadPath + File.separator + projectId + "_"
+ bidFile.getOriginalFilename();
filePath = filePath.replaceFirst("/", "");
System.out.println("ProjectPost save bid file path is:"
+ filePath);
uploadPath = "D:\\Workspaces\\" + uploadPath;
uploadPath = uploadPath.replaceFirst("/", "");
String uploadRealPath = "D:\\Workspaces\\" + filePath;
System.out.println("ProjectPost save bid file path is:"
+ uploadRealPath);
// 查看目录是否存在
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
bidFile.transferTo(new File(uploadRealPath));
System.out.println("Saving file: "
+ bidFile.getOriginalFilename());
0 0
原创粉丝点击