SpringMVC图片上传
2015-07-21 18:24
656 查看
配置文件为:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="200000" />
</bean>
controller层的编写:
@RequestMapping("/###")
public String tupianTest1(@RequestParam("photo") MultipartFile photo,HttpServletRequest request) throws IOException {
System.out.println("===into tupianTest1====");
// 取得上传文件的内容
byte[] content = null;
content = photo.getBytes();
System.out.println("上传内容" + content[1]);
// 取得上传到服务器上的文件路径
String path = request.getSession().getServletContext()
.getRealPath("/upload");
// LogUtil.debug(log, "上传图片的存储位置是:%s", path);
System.out.println("图片上传路径" + path);
// 判断上传路径是否存在
File uploadPath = new File(path);
if (!uploadPath.exists()) {
uploadPath.mkdir();
}
// 为上传的文件重命名,用vo中的id替代原始的文件名
String filename = photo.getOriginalFilename();
String exName = filename.substring(filename.lastIndexOf("."));
String newFileName = System.currentTimeMillis() + exName;
System.out.println("图片名字:" + newFileName);
// 组装上传文件新文件名及上传路径
String imgUrl = "/upload/" + newFileName;
System.out.println("上传路径:" + imgUrl);
// 执行上传
FileUtils.writeByteArrayToFile(new File(path + "/" + newFileName),
content);
System.out.println("上传成功!");
return "###";
}
jsp页面的编写:
<form action="###" method="post" enctype="multipart/form-data" >
<center>
<input type="file" name="photo" />
<button type="submit">提交</button>
</center>
</form>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="200000" />
</bean>
controller层的编写:
@RequestMapping("/###")
public String tupianTest1(@RequestParam("photo") MultipartFile photo,HttpServletRequest request) throws IOException {
System.out.println("===into tupianTest1====");
// 取得上传文件的内容
byte[] content = null;
content = photo.getBytes();
System.out.println("上传内容" + content[1]);
// 取得上传到服务器上的文件路径
String path = request.getSession().getServletContext()
.getRealPath("/upload");
// LogUtil.debug(log, "上传图片的存储位置是:%s", path);
System.out.println("图片上传路径" + path);
// 判断上传路径是否存在
File uploadPath = new File(path);
if (!uploadPath.exists()) {
uploadPath.mkdir();
}
// 为上传的文件重命名,用vo中的id替代原始的文件名
String filename = photo.getOriginalFilename();
String exName = filename.substring(filename.lastIndexOf("."));
String newFileName = System.currentTimeMillis() + exName;
System.out.println("图片名字:" + newFileName);
// 组装上传文件新文件名及上传路径
String imgUrl = "/upload/" + newFileName;
System.out.println("上传路径:" + imgUrl);
// 执行上传
FileUtils.writeByteArrayToFile(new File(path + "/" + newFileName),
content);
System.out.println("上传成功!");
return "###";
}
jsp页面的编写:
<form action="###" method="post" enctype="multipart/form-data" >
<center>
<input type="file" name="photo" />
<button type="submit">提交</button>
</center>
</form>
相关文章推荐
- eclipse 配置SVN代理服务器
- 3.第三单元任务二实训:使用作业提交系统提交Java作业时 ,需要输入Java源代码文件名和自己的邮箱,提交前对Java文件名以及邮箱进行有效检查。编写程序实现对输入的Java源文件名以及邮箱有效性的
- Java关于集合(list,set,map)的遍历与增强for循环的使用
- 常用Eclipse插件在线安装地址
- java反射机制入门01
- 开始java
- Java — 冒泡、选择、插入排序法
- java集群优化——数据源加密
- java怎样实现线程的同步
- java线程概述
- Java基础语法(二)(基本类型的转换,运算符)
- 数据表生成javabean工具
- Mybatis-Spring
- java单元测试工具--JUnit
- Java定时器,定时处理相关程序
- Java中类名与文件名的关系
- Java列出2到100之间所有素数的方法
- JAVA中int、String的类型转换
- struts2文件上传
- Hibernate&&Spring中的缩写英文术语