@RequestMapping("/insert")
@ResponseBody
public Object insert(HttpServletRequest request,
HttpServletResponse response, @RequestParam MultipartFile[] urlName)
throws IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String name=request.getParameter("name");
name=new String(name.getBytes("ISO-8859-1"),"utf-8");
System.out.println(name);
String title = request.getParameter("title");
title=new String(title.getBytes("ISO-8859-1"),"utf-8");
System.out.println(title);
String path = "";//定义路径
String fileName = urlName[0].getOriginalFilename();//获取上传第一个图片的名字
System.out.println(urlName[0].getOriginalFilename());
String ext = fileName.substring(fileName.lastIndexOf("."));//根据点截取
String bgimg = UUID.randomUUID().toString().replaceAll("-", "") + ext;
fileName = urlName[1].getOriginalFilename();
System.out.println(urlName[1].getOriginalFilename());
ext = fileName.substring(fileName.lastIndexOf("."));
String selectimg = UUID.randomUUID().toString().replaceAll("-", "")
+ ext;
for (int i = 0; i < urlName.length; i++) {
MultipartFile myfile = urlName[i];
if (myfile.isEmpty()) {
System.out.println("文件未上传");
} else {
path = request.getSession().getServletContext()
.getRealPath("image");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
// 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
// FileUtils.copyInputStreamToFile(myfile.getInputStream(),
// new File(path, myfile.getOriginalFilename()));
if (i == 0) {
FileUtils.writeByteArrayToFile(new File(path, bgimg),
myfile.getBytes());
} else if (i == 1) {
FileUtils.writeByteArrayToFile(new File(path, selectimg),
myfile.getBytes());
}
}
}
// @RequestParam MultipartFile[] urlName,
TplayerVersion tplayVersion = tplayerVersionService.selectName(name);
TplayerTheme tplayerTheme = new TplayerTheme();
tplayerTheme.setApp_version_id(tplayVersion.getId());
tplayerTheme.setTitle(title);
tplayerTheme.setBg_img("<img" + " " + "src='image/" + bgimg
+ "'" + ">");//把图片的路径插入到数据库,因为ext显示图片必须加上img标签才能显示图片
tplayerTheme.setSelected_img("<img" + " " + "src='image/"
+ selectimg +"'"+ ">");
tplayerTheme.setIs_on(true);
tplayerTheme.setSort(0);
Date date = new Date();
Timestamp nousedate = new Timestamp(date.getTime());
tplayerTheme.setCreatetime(nousedate);
tplayerTheme.setModifytime(nousedate);
tplayerThemeService.insert(tplayerTheme);
System.out.println("22222222222");
this.success = true;
String msg = "{success:true}";
return msg;
}
Spring mvc多图片上传然后在页面展示
最新推荐文章于 2022-12-10 23:00:02 发布