从HttpServletRequest中读取文件内容
Posted 十木禾
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从HttpServletRequest中读取文件内容相关的知识,希望对你有一定的参考价值。
这里介绍如何利用表单来将文件传输到服务器。
首先我们的前台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>上传文件</title>
</head>
<body>
<form action="file/fileUpload" enctype="multipart/form-data"
method="post">
<input type="file" name="fileUploader" multiple /><br />
<br /> <input type="submit" value="上传文件" />
</form>
</body>
</html>
web.xml
中的配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" id="WebApp_ID" version="2.5">
<!--配置servlet -->
<servlet>
<servlet-name>FileUploadController</servlet-name>
<servlet-class>com.blog.upload.FileUploadController
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadController</servlet-name>
<url-pattern>/file/fileUpload</url-pattern>
</servlet-mapping>
</web-app>
接下来就是我们后台的代码了
在这里我将从HttpServletRequest
中读取文件的方法封装成一个静态的方法,放在FilEUploadUtils.getFileContentFromRequest()
。
则控制器的代码如下
package com.blog.upload;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileUploadController extends HttpServlet
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
String content=FilEUploadUtils.getFileContentFromRequest(request); //获取文件的内容
System.out.println(content); //输出文件的内容
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
super.doGet(req, resp);
静态方法getFileContentFromRequest
中的代码如下
package com.blog.upload;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletRequest;
import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
public class FilEUploadUtils
public static String getFileContentFromRequest(HttpServletRequest request)
try
request.setCharacterEncoding("utf-8");
if(ServletFileUpload.isMultipartContent(request))
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();// 创建该对象
ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory);// 创建该对象
FileItemIterator itemIterator=fileUpload.getItemIterator(request);// 解析request 请求,并返回FileItemIterator集合
StringBuffer fileContent=new StringBuffer();
while(itemIterator.hasNext())
FileItemStream itemStream=itemIterator.next();//从集合中获得一个文件流
if(!itemStream.isFormField() && itemStream.getName().length() > 0) //过滤掉表单中非文件
BufferedInputStream inputStream=new BufferedInputStream(itemStream.openStream()); //获得文件输入流
inputStream.mark(itemStream.openStream().available()+1);
//判断上传的文件的编码格式 支持 UTF-8 和 ANSI两种类型
String charsetName=getFileCharsetName(inputStream);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,charsetName));
String lineTxt=null;
while((lineTxt=bufferedReader.readLine()) != null)
fileContent.append(lineTxt+"\\r");
return fileContent.toString();
catch(Exception e)
e.printStackTrace();
return null;
//判断上传的文件的编码格式 支持 UTF-8 和 ANSI两种类型
private static String getFileCharsetName(InputStream inputStream) throws IOException
String charsetName="GBK";
byte[] typeByte = new byte[3];
inputStream.read(typeByte);
if (typeByte[0] == -17 && typeByte[1] == -69 && typeByte[2] == -65)
charsetName="UTF-8";
else
charsetName="GBK";
inputStream.reset();
return charsetName;
以上是关于从HttpServletRequest中读取文件内容的主要内容,如果未能解决你的问题,请参考以下文章
HttpServletRequest中读取HTTP请求的body
解决HttpServletRequest InputStream只能读取一次问题