package com.java.controller;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.java.bean.Progress;
@Controller
public class FileUploadController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile[] file,
ModelMap model) {
System.out.println("开始");
String path = request.getSession().getServletContext().getRealPath("upload");
for (int i = 0; i < file.length; i++) {
String fileName = file[i].getOriginalFilename();
// String fileName = new Date().getTime()+".jpg";
System.out.println(path);
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file[i].transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileUrl", request.getContextPath() + "/upload/" + fileName);
}
return "result";
}
@RequestMapping("getprogress")
@ResponseBody
public void getProgress(HttpServletRequest request, HttpServletResponse response) throws IOException {
Progress progress = (Progress) request.getSession().getAttribute("status");
PrintWriter writer = response.getWriter();
System.err.println(progress);
NumberFormat numberFormat = NumberFormat.getInstance();
// 设置精确到小数点后2位
numberFormat.setMaximumFractionDigits(2);
String result = numberFormat
.format((float) progress.getBytesRead() / (float) progress.getContentLength() * 100);
writer.println(result + "%");
System.err.println(result + "%");
writer.flush();
writer.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论













收起资源包目录































































































共 74 条
- 1
资源评论

- Lore_lei2018-01-21一个比较完成的项目,只是没有多个文件上传的进度

何必暧昧
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
