利用MultipartFile实现文件上传
2018-02-05 12:00
323 查看
利用MultipartFile实现文件上传
在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的MultipartFile实现文件上传,感觉挺简单,在这里和大家分享一下.一.主要有两个java类,和一般的servlet放在一起即可.
1.FileUploadBean.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package chb.demo.web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.multipart.MultipartFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chb
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class FileUploadBean {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private MultipartFile file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setFile(MultipartFile file) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.file = file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public MultipartFile getFile() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
2.FileUploadController.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package chb.demo.web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.BindException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.multipart.MultipartFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.ModelAndView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.mvc.SimpleFormController;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chb
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class FileUploadController extends SimpleFormController {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected ModelAndView onSubmit(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpServletRequest request,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpServletResponse response,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object command,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
BindException errors){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// cast the bean
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileUploadBean bean = (FileUploadBean) command;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// let's see if there's content there
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MultipartFile file = bean.getFile();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (file == null) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("上传失败:文件为�空");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(file.getSize()>10000000)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("上传失败:文件大小不能超过10M");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//得到文件�名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String filename=file.getOriginalFilename();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(file.getSize()>0){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SaveFileFromInputStream(file.getInputStream(),"D:/",filename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("上传失败:上传文件不能为�空");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// well, let's do nothing
143fe
with the bean for now and return:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return super.onSubmit(request, response, command, errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch(Exception ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(ex.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**保存文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param stream
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param path
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param filename
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @throws IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] buffer =new byte[1024*1024];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int bytesum = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int byteread = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while ((byteread=stream.read(buffer))!=-1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytesum+=byteread;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fs.write(buffer,0,byteread);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fs.flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fs.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stream.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
二.配置文件中如下配置:
1.web.xml,利用spring mvc模式,大家应该都很熟悉了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<servlet>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<servlet-name>chb</servlet-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<load-on-startup>1</load-on-startup>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</servlet>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<servlet-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<servlet-name>chb</servlet-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<url-pattern>*.do</url-pattern>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</servlet-mapping>
2.chb-servlet.xml,这里要配置映射,并可以设定最大可上传文件的大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Multi-Action 用来标识method的变量名定义-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="paramName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>action</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="defaultMethodName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>index</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- one of the properties available; the maximum file size in bytes -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="maxUploadSize" value="10000000"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="mappings">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="/upload.do">fileUploadController</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="fileUploadController" class="chb.demo.web.FileUploadController">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="commandClass" value="chb.demo.web.FileUploadBean"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- 上传失败时跳转页面 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="formView" value="/user/err.jsp"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- 上传成功时跳转页面 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="successView" value="/user/confirmation.jsp"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</beans>
三.设定jsp页面
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" method="post" action="upload.do" enctype="multipart/form-data">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td width="25%" align="right">上传文件:</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td><input id="file" type="file" NAME="file" style="width:300px;"></td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr align="center" valign="middle">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td height="60" colspan="2"><input type="submit" ID="BtnOK" value="确认上传"></td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
ok,现在就可以上传文件了,挺简单吧?这里我只列出了基本步骤,至于具体的操作(比如中文问题)可能就需要大家自己再完善完善了.
相关文章推荐
- 利用MultipartFile实现文件上传
- 利用MultipartFile实现文件上传功能
- 利用MultipartFile实现文件上传
- 利用MultipartFile实现文件上传
- Java中利用MultipartFile实现文件上传
- 利用MultipartFile实现文件上传
- 利用MultipartFile实现文件上传
- 利用spring的MultipartFile实现文件上传【原】
- 利用MultipartFile实现文件上传
- 利用MultipartFile实现文件上传(springMVC中)
- 利用Hadoop的FileSystem create方法获取 FSDataOutputStream 实现文件的上传
- 利用resteasy框架构建rest webservice----第三波:实现文件上传---非form提交方式(实例、教程)
- Spring MVC - MultipartFile实现文件上传(单文件与多文件上传)
- 利用iframe来实现ajax文件上传
- 利用WebUploader实现大文件上传和视频上传
- 利用Nginx的上传模块和上传进度模块实现网页上传文件
- 利用commons-fileupload实现文件的上传(源码)
- PHP利用APC模块实现文件上传进度条的方法
- struts2 利用fileupload实现文件上传
- iOS- 利用AFNetworking3.0+(最新AFN) - 实现文件上传