package com.javaniu.web; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/common") public class CommonController { // 部署目录,修改为自己的 private @Value("${deploy.path}") String deployPath; // 上传目录,修改为自己的 private @Value("${upload.path}") String uploadPath; // 域名,修改为自己的 private @Value("${javaniu.domain}") String javaniuDomain; @RequestMapping(value = { "test" }, method = { RequestMethod.GET }) public ModelAndView upload(HttpSession session) { System.out.println("test get:" + session.getId()); ModelAndView modelAndView = new ModelAndView("common/test"); return modelAndView; } /** * 通过apache commons.fileupload接收上传 * * @param file * @param response */ @RequestMapping(value = { "upload" }, method = { RequestMethod.POST }) public void upload(@RequestParam("file") CommonsMultipartFile file, HttpSession session, HttpServletResponse response) { System.out.println("upload post:" + session.getId()); // 将该file放到session中以便客户端定时可以从session中获取到该对象的上传进度,不支持单客户端多文件上传,session会覆盖掉file // 要想支持多文件可以给客户端分配一个唯一标识即可,然后客户端请求时带上该标识从session中获取对应的对象即可,即:session.getAttribute(唯一标识) session.setAttribute("file", file); response.setContentType("text/html;charset=UTF-8"); File uploadDir = new File(deployPath + uploadPath); if (!uploadDir.exists()) {// 不存在则创建 uploadDir.mkdirs(); } String name = System.currentTimeMillis() + ".jpg"; String localPath = deployPath + uploadPath + name; String url = javaniuDomain + uploadPath + name; try { file.transferTo(new File(localPath)); PrintWriter writer = response.getWriter(); writer.print(url); writer.close(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 获取上传进度 * * @param file * @param response * @throws Exception */ @RequestMapping(value = "process") public void process(HttpSession session, HttpServletResponse response) { System.out.println("process get:" + session.getId()); // 上传进度百分比 long processPercent = 0; CommonsMultipartFile file = (CommonsMultipartFile) session .getAttribute("file"); if (file == null) { return; } long totalFileSize = file.getSize(); long readedFileSize = file.getFileItem().getSize(); System.out.println("totalFileSize:" + totalFileSize + ",readedFileSize:" + readedFileSize); if (totalFileSize != 0) { processPercent = Math.round(readedFileSize / totalFileSize) * 100; } response.setContentType("text/html;charset=UTF-8"); PrintWriter writer; try { writer = response.getWriter(); writer.print(processPercent); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }

wjh12345654321 LV14
2022年8月25日
wrg19970615 LV4
2019年11月12日
912299793 LV21
2018年7月13日
guolifeng LV6
2017年9月30日
liangzai123 LV18
2017年1月3日
621726 LV15
2016年3月1日
zhudiyuan LV13
2015年10月15日
samliao LV13
2015年8月14日
神器神马 LV1
2015年8月11日
liutaols LV8
2015年5月11日