java中的接口是类吗
319
2023-03-15
SpringMVC上传文件的两种方法
在该示例中,阐述了SpringMVC如何上传文件。
1、上传页面upload.jsp
file:
2、controller配置文件
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:p="http://springframework.org/schema/p" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:p="http://springframework.org/schema/p"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.0.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.0.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.0.xsd">
主要是添加了文件上传的解析器,配置了默认编码,最大的上传大小以及缓存大小等参数。
3、Controller
package cn.com.yy.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputSACuesxIrljtream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
@RequestMapping("/data")
public class FileUploadController {
/**
* method1:通过参数CommonsMultipartFile进行解析
* @RequestParam("file")中的file对应于upload.jsp中的file类型的name对应的名称
* @param file
* @return
* @throws IOException
*/
@RequestMapping(value="/uploadfile")
public String upload1(@RequestParam("file") CommonsMultipartFile file) throws IOException{
//获取文件名称
String fileName = file.getOriginalFilename();
//写入本地磁盘
InputStream is = file.getInputStream();
byte[] bs = new byte[1024];
int len;
OutputStream os = new FileOutputStream(new File("D:/temp/" + fileName));
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
return "upload_success";
}
@RequestMapping("/upload")
public String toPage(){
return "upload";
}
}
4、返回页面upload_success.jsp
upload file success!!
5、测试
访问 http://localhost:8080/TestSpringMVC3/data/upload 跳转到上传页面
选择文件上传
点击上传会跳转到上传成功页面。
上述方法只是简单的讲解了SpringMVC如何上传文件。
第二种方法:使用SpringMVC封装的方法进行文件上传
/**
* 使用SpringMVC封装好的方法进行文件上传
* @param request
* @param response
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping("/uploadfile2")
public void upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
//获取解析器
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断是否是文件
if(resolver.isMultipart(request)){
//进行转换
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
//获取所有文件名称
Iterator
while(it.hasNext()){
//根据文件名称取文件
MultipartFile file = multiRequest.getFile(it.next());
String fileName = file.getOriginalFilename();
String localPath = "D:/temp/" + fileName;
File newFile = new File(localPath);
//上传的文件写入到指定的文件中
file.transferTo(newFile);
}
}
}
该方法上传文件效率更优。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~