Spring中servletFileUpload完成上传文件以及文本的处理

时间:2023-01-27 21:10:23

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test.do" method="post" enctype="multipart/form-data">
名字:<input name="username" type="text"><br />
<input name="imgFile" id="imgFile" type="file" /><br />
<input type="submit" value="提交">
</form>
</body>
</html>

  JAVA:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class tt
{
@RequestMapping(value = "test")
public void test(HttpServletRequest request)
{
boolean flag = ServletFileUpload.isMultipartContent(request); if (flag)
{
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
// 解析request请求
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list)
{
System.out.println(item); String fileName = item.getFieldName();
// 判断是否是普通的输入项
if (item.isFormField())
{
// 解析文本
if (fileName.equals("username"))
{
try
{
String username = item.getString("UTF-8");
System.out.println(username);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
}
// 上传图片
if (!item.isFormField())
{
InputStream in = null;
FileOutputStream out = null;
try
{
in = item.getInputStream();
out = new FileOutputStream(new File("E:/test.txt")); byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) != -1)
{
out.write(buff, 0, len);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
} }
}
}
}
catch (FileUploadException e)
{
e.printStackTrace();
} } }
}

  在Spring中不用配置:否则会导致request请求两次解析造成解析为空值的问题(详情可以参考:http://blog.csdn.net/lwphk/article/details/43015829)

<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value ="1024" />
<property name="resolveLazily" value="true"/>
<property name="defaultEncoding" value = "UTF-8" />