java 上传文件的处理(MultipartFile file)
2017-10-27 10:30
471 查看
jsp页面
<%@page language="java" pageEncoding="UTF-8"%>
<div style="" id="importFormBox">
<form target="importUploadFrame"enctype="multipart/form-data" method="post" action="messagetemplate/check_import" id="importForm" >
<div class="inputfield">请选择您要导入的文件:
<input type="file" size="22" name="file" />
<button class="button" style="padding: 1px 1px;" type="submit">开始导入</button>
</div>
<div style="display: none;" class="progress"></div>
</form>
<div class="import_error_msg" style="display:none">
</div>
<div>
<iframe frameborder="0" scrolling="no" name="importUploadFrame" src="about:blank"></iframe>
</div>
</div>
java代码
@RequestMapping( value = "/check_import", method = RequestMethod.POST, produces = "text/plain; charset=UTF-8" )
@ResponseBody
public String do_check_import( HttpServletRequest request,MultipartFile file ) throws Exception
{
Operator currentOperator = this.getCurrentOperator( request.getSession() );
try
{
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename();
System.out.println( filePath );
// 转存文件
file.transferTo(new File(filePath));
Map<String, Object> ret = new HashMap<>();
ret.put( "status", "ok" );
ret.put( "data", data );
return JSON.toJSONString( ret );
}
catch( Exception e )
{
Map<String, Object> ret = new HashMap<>();
ret.put( "status", "ok" );
ret.put( "data", data );
return JSON.toJSONString( e.getMessage() );
}
}
配置 servlet-context.xml(如果没有这个配置,获取的文件是空的)
<!-- 上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<!-- 最大内存大小 -->
<property name="maxInMemorySize" value="10240"/>
<!-- 最大文件大小,-1为不限制大小 -->
<property name="maxUploadSize" value="102400000"/>
</bean>
配置 pom.xml
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<%@page language="java" pageEncoding="UTF-8"%>
<div style="" id="importFormBox">
<form target="importUploadFrame"enctype="multipart/form-data" method="post" action="messagetemplate/check_import" id="importForm" >
<div class="inputfield">请选择您要导入的文件:
<input type="file" size="22" name="file" />
<button class="button" style="padding: 1px 1px;" type="submit">开始导入</button>
</div>
<div style="display: none;" class="progress"></div>
</form>
<div class="import_error_msg" style="display:none">
</div>
<div>
<iframe frameborder="0" scrolling="no" name="importUploadFrame" src="about:blank"></iframe>
</div>
</div>
java代码
@RequestMapping( value = "/check_import", method = RequestMethod.POST, produces = "text/plain; charset=UTF-8" )
@ResponseBody
public String do_check_import( HttpServletRequest request,MultipartFile file ) throws Exception
{
Operator currentOperator = this.getCurrentOperator( request.getSession() );
try
{
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename();
System.out.println( filePath );
// 转存文件
file.transferTo(new File(filePath));
Map<String, Object> ret = new HashMap<>();
ret.put( "status", "ok" );
ret.put( "data", data );
return JSON.toJSONString( ret );
}
catch( Exception e )
{
Map<String, Object> ret = new HashMap<>();
ret.put( "status", "ok" );
ret.put( "data", data );
return JSON.toJSONString( e.getMessage() );
}
}
配置 servlet-context.xml(如果没有这个配置,获取的文件是空的)
<!-- 上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<!-- 最大内存大小 -->
<property name="maxInMemorySize" value="10240"/>
<!-- 最大文件大小,-1为不限制大小 -->
<property name="maxUploadSize" value="102400000"/>
</bean>
配置 pom.xml
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
相关文章推荐
- java 基于List<MultipartFile>实现多文件上传
- java上传文件将MultipartFile类型装换为File类型
- JAVA Server上传文件 Spring MultipartResolver 或者 ServletFileUpload
- lamp使用php处理上传文件,调用move_uploaded_file函数遇到目录写权限问题及解决过程
- asp.net文件上传用到html的file控件,在使用母版页Master,更改form的Enctype=multipart/form-data的方法
- java中文件上传的处理
- flex_文件上传处理JAVA类;
- AjaxFileUpLoad 文件异步上传Demo(模仿官方网站,后台使用.net一般处理程序)
- spring mvc MultipartFile 上传文件错误解决
- 使用multipart请求处理文件上传
- 使用multipart请求处理文件上传
- java /Spring /jsp 使用input标签 file 如何上传多个文件
- 上传文件jsp报错An error occurred at line: xx in the generated java file
- JAVA Server上传文件 Spring MultipartResolver 或者 ServletFileUpload
- JAVA中的文件及目录处理类--File
- java输入输出处理-文件file
- java fileupload上传文件
- ASP.NET 用.Net的File控件上传文件大文件上传之异常处理 的解决方案
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- 上传文件 时FileNotFoundException:java.io.FileNotFoundException