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();
}
}
}
最近下载更多
sswert LV2
2024年3月13日
推墙大师 LV1
2024年1月2日
奈奈子 LV1
2023年5月31日
ming_123_9715 LV23
2022年7月17日
hodor^O^ LV1
2021年11月20日
珊 LV1
2021年11月12日
白夜 LV1
2021年10月22日
nbzhou2013 LV14
2021年9月11日
qq70081337 LV6
2021年8月26日
1973356987 LV13
2021年6月18日

最近浏览