最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
React,Native封装Toast,React,Native,加载Loading组件
时间:2018-09-10 编辑:猪哥 来源:一聚教程网
本文为大家分享了webuploader springmvc实现图片上传的具体代码,供大家参考,具体内容如下
jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here css" href="${pageContext.request.contextPath}/manage/Widget/webuploader/0.1.5/webuploader.css" rel="external nofollow" >图片上传
选择文件
springMvc 的 servlet加入以下代码(允许上传):
引入的包
commons-io-1.3.2.jar
commons-fileupload-1.2.1.jar
java代码
package com.shopping.controller; import java.io.File; import java.io.IOException; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; /** * @author MAZN * @date 创建时间:2017年5月2日 下午10:02:36 * @parameter * @return */ @Controller public class UploadImgController { int counter = 0; @RequestMapping("/uploader") public void upload(HttpServletRequest request,HttpServletResponse response){ //String fileName; // File tagetFile; System.out.println("收到图片!"); MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest)request; Mapfiles = Murequest.getFileMap();//得到文件map对象 //String upaloadUrl = request.getSession().getServletContext().getRealPath("/")+"upload/";//得到当前工程路径拼接上文件名 String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); int num=t.indexOf(".metadata"); String small = "small"; String upaloadUrl=t.substring(1,num).replace('/', '\')+"image\"+small+"\"; //+"项目名\WebContent\文件"; File dir = new File(upaloadUrl); System.out.println(upaloadUrl); String img_url = upaloadUrl;//图片路径 if(!dir.exists())//目录不存在则创建 dir.mkdirs(); for(MultipartFile file :files.values()){ counter++; String fileName=file.getOriginalFilename(); File tagetFile = new File(upaloadUrl+fileName);//创建文件对象 img_url += fileName; if(!tagetFile.exists()){//文件名不存在 则新建文件,并将文件复制到新建文件中 try { tagetFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { file.transferTo(tagetFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(img_url); System.out.println("接收完毕"+counter); } }
相关文章
- SpringBoot自定义bean绑定解析 10-24
- Javaweb工程运行报错HTTP Status 404解决教程 10-20
- JAVA获取jvm和操作系统相关信息方法 10-20
- BeanFactory和FactoryBean的区别讲解 10-20
- 微信小程序的宿主环境实现教程 10-10
- dispatchEvent解决重叠元素响应事件教程 10-10