java各种文件上传技术汇总

来源:互联网 发布:java json 美化 编辑:程序博客网 时间:2024/05/16 14:04

一、Servlet 

1、普通的上传:

https://my.oschina.net/Barudisshu/blog/150026

2、ajax上传:

前台采用 ajaxfileupload.js,

http://blog.csdn.net/u011783224/article/details/52862162 

二、Spring MVC

配置文件

<!-- SpringMVC上传文件时,需配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过800KB......注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="800000"/>
<property name="defaultEncoding" value="utf-8"></property>  
</bean>

<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
</props>
</property>
</bean>

前台

<form id="deployForm" action="${pageContext.request.contextPath}/deploy/deployQuery.do" method="post"
     enctype="multipart/form-data">

后台

/**
* 这里这里用的是MultipartFile[] myfiles参数,所以前台就要用<input type="file" name="myfiles"/>
* 上传文件完毕后返回给前台[0`filepath],0表示上传成功(后跟上传后的文件路径),1表示失败(后跟失败描述)
*/
@RequestMapping("/uploadDelpoyFile")
public String uploadDelpoyFile(@RequestParam String deployName,
@RequestParam MultipartFile[] myfiles,HttpServletRequest request,
HttpServletResponse response) throws IOException {
 
//如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\upload\\文件夹中
//这里实现文件上传操作用的是commons.io.FileUtils类,它会自动判断/upload是否存在,不存在会自动创建
String path = request.getSession().getServletContext().getRealPath("/");
// String realPath = path.split("webapps")[0]+"webapps/upload/";
//upload/fas/deploy/
String realPath = path+File.separator + "upload"+File.separator+"fas"+File.separator+"deploy";

//设置响应给前台内容的数据格式
response.setContentType("text/html; charset=UTF-8");
//设置响应给前台内容的PrintWriter对象
PrintWriter out = response.getWriter();
//上传文件的原名(即上传前的文件名字)
String originalFilename = null;
try{

//如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
//如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且要指定@RequestParam注解
for(MultipartFile myfile : myfiles){
if(myfile.isEmpty()){
out.print(ReturnMessageUtil.RETURN_FAILED+"`请选择文件后上传");
out.flush();
}else{
if(!"application/x-zip-compressed".equals(myfile.getContentType())){
out.print(ReturnMessageUtil.RETURN_FAILED+"`文件类型不对,必须是zip");
out.flush();
}
originalFilename = myfile.getOriginalFilename();


try {
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉
//此处也可以使用Spring提供的MultipartFile.transferTo(File dest)方法实现文件的上传
FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, originalFilename));
} catch (IOException e) {
log.error("DeployController uploadDelpoyFile() error:"+e.getMessage());
e.printStackTrace();
out.print(ReturnMessageUtil.RETURN_FAILED+"`文件上传失败,请重试!!");
out.flush();
}
}
}

//如果上传成功的情况下就部署
deployService.deploy(deployName,originalFilename,realPath);

out.print(ReturnMessageUtil.RETURN_SUCCESS);
out.flush();
}catch(Exception e){
log.error("DeployController2 uploadDelpoyFile() error:"+e.getMessage());
e.printStackTrace();
out.print(ReturnMessageUtil.RETURN_FAILED+"`"+e.getMessage());
out.flush();

}
return null;
}



0 0
原创粉丝点击