spring mvc 批量上传+文件上传
来源:互联网 发布:pos机小票打印软件 编辑:程序博客网 时间:2024/05/16 06:11
spring mvc 批量上传+文件上传
简单3步走。搞定!
上传文件成功后:
1 上传文件核心方法
- public static String saveWebImgFile(MultipartFile imgFile){
- String webFilePath = "";
- if(imgFile.getSize() > 0 && isImage(imgFile.getContentType())){
- FileOutputStream fos = null;
- try {
- byte[] b = imgFile.getBytes();
- /* 构造文件路径 */
- String webRoot = PathUtil.classPath();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
- SimpleDateFormat sdf2 = new SimpleDateFormat("HHmmssSSS");
- String datePath = sdf.format(new Date());
- String datePath2 = sdf2.format(new Date());
- String dirPath = webRoot + "/../../upload/img/" + datePath;
- File dir = new File(dirPath);
- if(!dir.exists()){
- dir.mkdirs();
- }
- String fileName = imgFile.getOriginalFilename();
- String filePath = dirPath + "/" + datePath2 + getImgSuffix(fileName);
- webFilePath = "/upload/img/" + datePath + "/" + datePath2 + getImgSuffix(fileName);
- File file = new File(filePath);
- dir.setWritable(true);
- file.setWritable(true);
- fos = new FileOutputStream(file);
- fos.write(b);
- } catch (IOException e) {
- throw new RuntimeException("文件上传失败!" ,e);
- }finally{
- if(fos != null){
- try {
- fos.close();
- } catch (IOException e) {
- throw new RuntimeException("文件上传->输出流关闭失败!!!!" ,e);
- }
- }
- }
- }
- return webFilePath;
- }
- @RequestMapping(value = "/imgupload")
- public String handleFormUpload(MultipartHttpServletRequest request) throws Exception{
- List<MultipartFile> files = request.getFiles("file");
- Map<String, String> imgs = new HashMap<String, String>();
- for (int i = 0; i < files.size(); i++) {
- String webpath = FileUtil.saveWebImgFile(files.get(i));
- imgs.put("img"+i, webpath);
- }
- request.setAttribute("imgs", imgs);
- return showImageList(request);
- }
3 页面代码
- <body>
- 商品图片设置:
- <form method="post" enctype="multipart/form-data" action="product/imgupload" name="imagefrm">
- <table>
- <tr>
- <td>商品大图:默认宽度600px,高度600px</td>
- </tr>
- <tr>
- <td><input type="file" name="file"/><c:if test="${imgs.img0!=null&&imgs.img0!=''}"><img src="<%=basePath %>${imgs.img0 }" style="width:40;height:40px;"/></c:if></td>
- </tr>
- <tr>
- <td>添加多角度图片:默认宽度50px,高度50px</td>
- </tr>
- <tr>
- <td><input type="file" name="file"/><c:if test="${imgs.img1!=null&&imgs.img1!=''}"><img src="<%=basePath %>${imgs.img1 }" style="width:40;height:40px;"/></c:if></td>
- </tr>
- <tr>
- <td><input type="file" name="file"/><c:if test="${imgs.img2!=null&&imgs.img2!=''}"><img src="<%=basePath %>${imgs.img2 }" style="width:40;height:40px;"/></c:if></td>
- </tr>
- <tr>
- <td><input type="file" name="file"/><c:if test="${imgs.img3!=null&&imgs.img3!=''}"><img src="<%=basePath %>${imgs.img3 }" style="width:40;height:40px;"/></c:if></td>
- </tr>
- <tr>
- <td><input type="file" name="file"/><c:if test="${imgs.img4!=null&&imgs.img4!=''}"><img src="<%=basePath %>${imgs.img4 }" style="width:40;height:40px;"/></c:if></td>
- </tr>
- <tr>
- <td><input type="submit" value="上传所有图片"/></td>
- </tr>
- </table>
- </form>
- </body>
0 0
- spring mvc 批量上传+文件上传
- spring mvc 批量上传+文件上传
- spring mvc 批量上传+文件上传
- Spring mvc批量图片上传
- spring mvc 文件上传
- spring MVC 文件上传
- Spring MVC文件上传
- Spring MVC文件上传
- spring mvc 文件上传
- Spring mvc 文件上传
- spring mvc 文件上传
- spring mvc 上传文件
- spring mvc 上传文件
- spring mvc 文件上传
- spring mvc 上传文件
- spring mvc文件上传
- Spring MVC 文件上传
- spring mvc 文件上传
- 操作系统面试题
- java套接字---0
- 操作系统常见面试题总结
- Eclipse 常用快捷键 常用技巧My Eclipse常用快捷键
- spring mvc +Mybatis3.1 整合的时候异常
- spring mvc 批量上传+文件上传
- java 短连接+MD5加密短链接
- java 图片 批量 压缩 +全部压缩
- Lucene用到的oscache,记录下来
- Testing Is the Engineering Rigor of Software Development
- 数字精确运算BigDecimal常用方法
- Thinking in States
- 数字精确运算BigDecimal常用方法
- solr 3.5 配置及服务器设置