@RequestMapping(value = "/manage/traUploadImg")
public class TraUploadImgController {
// private final Logger LOGGER = LoggerFactory
// .getLogger(TraUploadImgController.class);
@RequestMapping("/main")
public String main() {
return "manage/traUploadImg/traUploadImgMain";
}
@RequestMapping("/eagleEye")
public String eagleEye() {
return "manage/eagleEye/manager";
}
@RequestMapping("/editDlg")
public String editDlg() {
return "manage/traUploadImg/traUploadImgEditDlg";
}
@ResponseBody
@RequestMapping(value = "/appUploadImg")
public String uploadImg(
@RequestParam("uploadfile") CommonsMultipartFile files[],
TraUploadImg traUploadImg, HttpServletRequest request) {
StringBuilder urlsStr = new StringBuilder();
SimpleDateFormat datef = new SimpleDateFormat("yyyy");
SimpleDateFormat datef1 = new SimpleDateFormat("MM-dd");
SimpleDateFormat timef = new SimpleDateFormat("HH_mm_ss");
Date dateNow = new Date();
String date = datef.format(dateNow);
String date1 = datef1.format(dateNow);
String time = timef.format(dateNow);
List
// 获得项目的路径
ServletContext sc = request.getSession().getServletContext();
// 上传位置
String path = request.getSession().getServletContext().getRealPath("/") + "/uploadImg/" + date +"/"+date1; // 设定文件保存的目录
System.out.println("文件保存目录为:"+path);
File f = new File(path);
if (!f.exists())
f.mkdirs();
for (int i = 0; i < files.length; i++) {
// 获得原始文件名
String fileName = files[i].getOriginalFilename();
System.out.println("原始文件名:" + fileName);
// 新文件名
String newFileName = time + "_" + fileName;
if (!files[i].isEmpty()) {
try {
String filePath = path + "/" + newFileName;
FileOutputStream fos = new FileOutputStream(filePath);
InputStream in = files[i].getInputStream();
int b = 0;
while ((b = in.read()) != -1) {
fos.write(b);
}
fos.close();
in.close();
urlsStr.append("," + date + "/" + newFileName);
System.out.println("上传图片到:" + filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
list.add(path + newFileName);
}
traUploadImg.setImgUrls(urlsStr.toString().substring(1));
System.out.println("url"+traUploadImg.getImgUrls());
traUploadImg.setUploadTime(dateNow);
return "success";
}
}