Spring Uploading Files
2015-08-18 11:47
519 查看
1,在servlet-dispatcher.xml中添加代码
<beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>也可以根据需求添加相关属性<property name="maxUploadSize" value="2097152"></property>2,添加依赖jar文件
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency>
3、编写uploadController
@RequestMapping(value="/upload",method=RequestMethod.POST)public String processUpload(@RequestParam("name") String name,@RequestParam("file") MultipartFile file) throws IOException {log.info("File '" + file.getOriginalFilename() + "' uploaded successfully");if (!file.isEmpty()) {try {byte[] bytes = file.getBytes();BufferedOutputStream stream =new BufferedOutputStream(new FileOutputStream(new File(name)));stream.write(bytes);stream.close();return "You successfully uploaded " + name + "!";} catch (Exception e) {return "You failed to upload " + name + " => " + e.getMessage();}} else {return "You failed to upload " + name + " because the file was empty.";}}
4、使用RestClient测试上传
后台提示:[2015-08-18 11/:36/:12]INFO com.kaishuhezi.api.hardware.log.controller.LogController(line/:40) -File '78a0f7dcjw1e1bvyuzt1jj.jpg' uploaded successfully相关文章推荐
- Spring Uploading Files
- java中set集合的使用方法
- SpringMVC注解@RequestParam全面解析
- JAVA 可变参数
- java中list按照某个属性排序方法
- Java的自增陷阱
- dwr 配置文件 之web.xml
- Java 之 商品条形码的验证
- 去哪网实习总结:递归构建“流程执行顺序”的XML文件(JavaWeb)
- Java 数组之冒泡排序
- 双色球 Java
- Java中抽象的总结
- eclipse 动态安装插件
- Spring mvc 设置自定义首页
- 转载自rhino的Java enum的用法详解
- java获得执行jar的运行路径
- java逻辑运算符
- eclipse 快捷键
- json转成java对象
- java对象转化为JSON格式