实现文件上传功能
现在随着网站的人性化提升,提高用户的个性化体验,所以很多项目中都少不了文件和图片上传功能。上传图片功能,我们可以用来实现用户上传头像的功能,提高用户的个性化体验,本文将实现图片和文件上传的功能。
对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具common-fileupload这个文件上传组件。这个common-fileupload上传组件的jar包可以去apache官网上面下载,也可以在struts的lib文件夹下面找到,struts上传的功能就是基于这个实现的。common-fileupload是依赖于common-io这个包的,所以还需要下载这个包。
首先看html页面——————UploadFile.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="UploadFile.do" method="post" enctype="multipart/form-data">
<input type="text" name="username"> <br>
<input type="file" name="userimg">
<input type="submit" value="提交">
</form>
</body>
</html>
接下来看后台处理 ————UploadFile.java
import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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; @WebServlet("/UploadFile.do") public class UploadFile extends HttpServlet { private static final long serialVersionUID = 1L; public UploadFile() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //借助工具解析 Commons——fileupload smartupload //判断传递的是否是文件内型 boolean isMultipart =ServletFileUpload.isMultipartContent(request); if(isMultipart){ DiskFileItemFactory factory=new DiskFileItemFactory(); ServletContext servletContext =this.getServletConfig().getServletContext(); //获取临时文件夹 File repository =(File) servletContext.getAttribute("javax.servlet.context..tempdir"); factory.setRepository(repository); ServletFileUpload upload =new ServletFileUpload(factory); //解决中文乱码 upload.setHeaderEncoding("utf-8"); //解析request获取到上传的参数 try { List<FileItem> items=upload.parseRequest(request); //进行参数处理 for(FileItem item:items){ if(item.isFormField()){ System.out.println(item.getFieldName()); System.out.println(item.getString("utf-8")); }else{ System.out.println(item.getFieldName()); System.out.println(item.getName()); System.out.println(item.getContentType()); System.out.println(item.getSize()); //写入文件 "//"表示绝对路径 String rootPath = servletContext.getRealPath("//"); System.out.println(rootPath); String savaPath =rootPath+File.separator+"upload"; File fileSavaFolder =new File(savaPath); if(!fileSavaFolder.exists()){ fileSavaFolder.mkdirs(); } //完整的存储文件名 String savaFileName=savaPath+File.separator+item.getName(); System.out.println("name:"+savaFileName); File uploadedFile =new File(savaFileName); try { item.write(uploadedFile); } catch (Exception e) { e.printStackTrace(); } } } } catch (FileUploadException e) { e.printStackTrace(); } } } }
通过以上代码 我们就实现了文件上传功能