java实现文件的上传功能
本文实例为大家分享了java实现文件的上传功能的具体代码,供大家参考,具体内容如下
直接上代码了
一、上传界面
<%@ 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> <!-- 文件上传对表单的要求 1、表单中的请求和提交的方式必须是post 2、 表单中应该指定提交的请求为multipart 请求 ,通过在form表单中添加enctype属性 为multipart/form-data 3、表单中要有file元素 --> <!-- ${pageContext.request.contextPath}/registerServlet --> <form action="${pageContext.request.contextPath }/registerServlet" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="name" /><br> <hr> 年龄:<input type="text" name="age" /><br> <hr> 照片 :<input type="file" name="photo"/> <br> <hr> <input type="submit" value="注册"> <hr> </form> </body> </html>
二、Servlet处理
package com.ui.servlet; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; 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.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.tomcat.jni.OS; import com.mysql.jdbc.Field; /** * Servlet implementation class RegisterServlet */ @WebServlet("/registerServlet") public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub if(!ServletFileUpload.isMultipartContent(request)) { throw new RuntimeException("当前不支持文件上传"); } try { DiskFileItemFactory factory=new DiskFileItemFactory(); //设置使用临时文件的边界值,大鱼该值得文件上传会先保存在临时文件中,否则上传文件会直接写入到内存当中去 //单位 字节 在这里设置的为1m factory.setSizeThreshold(1024*1024*1); //设置临时文件 String temppath=this.getServletContext().getRealPath("/temp"); File file2=new File(temppath); factory.setRepository(file2); //创建文件上传核心组件 ServletFileUpload fileUpload=new ServletFileUpload(factory); //设置单个文件的大小不超过多少 fileUpload.setFileSizeMax(1024*1024*2); //上传多个文件的要求总的大小不超过多少 fileUpload.setSizeMax(1024*1024*5); fileUpload.setHeaderEncoding("utf-8"); //设置每一个item的字符编码为utf-8 List<FileItem> items=fileUpload.parseRequest(request); for (FileItem fileItem : items) { if(fileItem.isFormField()) { String filename=fileItem.getFieldName(); String fileValue=fileItem.getString("utf-8"); System.out.println(filename+"----"+fileValue); } else //如果是文件上传表单项 { String filename=fileItem.getName(); InputStream inputStream=fileItem.getInputStream(); //获取表单项的输入流 String path=this.getServletContext().getRealPath("/fileContents"); File file=new File(path,filename); OutputStream outputStream=new FileOutputStream(file); int len=-1; byte[] bs=new byte[1024]; while((len=inputStream.read(bs))!=-1) { outputStream.write(bs,0,len); } outputStream.close(); inputStream.close(); //删除临时文件 fileItem.delete(); } } } catch (Exception e) { // TODO: handle exception System.out.println(e); } } }
三、实现结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)