jsp+servlet简朴实现上传文件成果(生存目次改造)

<%-- Created by IntelliJ IDEA. User: Lenovo Date: 2020/6/19 Time: 22:53 Learn from https://www.bilibili.com/video/BV18z411i7gh?t=23&p=192 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件上传</title> </head> <body> <!--文件上传对表单的要求--> <!-- 1、表单中的请求提交方法必需是POST 2、表单中应指定所提交的请求位multipart请求,通过在<form/>标签中添加enctype属性 其值为multipart/form-data 3、 表单 --> <form method="POST" action="http://localhost:8888/hello/UploadImageServlet" enctype="multipart/form-data"> 编号<input type="text"></br> 名字<input type="text"></br> 照片<input type="file"></br> <input type="submit" value="注册"> </form> </body> </html>

2、servlet靠山

package Servlet.bookServlet; 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 javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; @WebServlet(name = "UploadImageServlet") public class UploadImageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、判定请求是不是multipart请求 if(!ServletFileUpload.isMultipartContent(request)){ throw new RuntimeException("当前请求不支持文件上传"); } System.out.println("开始上传文件"); //2、建设FileItem工场==>文件写入硬盘的浸染 try { DiskFileItemFactory factory = new DiskFileItemFactory(); //3、建设temp姑且文件夹 String tempPath ="D:\\tomcat\\apache-tomcat-9.0.35-windows-x64\\apache-tomcat-9.0.35\\webapps\\librarySystem\\web\\net\\temp"; File tempFile = new File(tempPath); factory.setRepository(tempFile); //4、配置利用姑且文件的界线值,大于该值,上传文件先生存在姑且文件中,小于该值,则直接写入内存 //单元是字节 factory.setSizeThreshold(1024*1024*1); //5、建设文件上传焦点组件 // 挪用ServletFileUpload.parseRequest要领理会request工具,获得一个生存了所有上传内容的List工具。 ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("utf-8");//可以办理文件名中文乱码 upload.setFileSizeMax(1024*1024*2); String bNo="defaultBNo",bName="defaultBName"; //6、理会请求 List<FileItem> items =upload.parseRequest(request); //7、遍历请求 for(FileItem item:items){ //普通表单项,上传名字,编号等普通信息的上i传 if(item.isFormField()){ String fileName = item.getFieldName();// name属性值 String fileValue = item.getString("utf-8");// name对应的value值 System.out.println(fileName + " -- " + fileValue); if(fileName.equalsIgnoreCase("BNO")){ bNo = fileValue; } if(fileName.equalsIgnoreCase("BNAME")){ bName = fileValue; } } else{//上传图片等 String fileName = item.getName(); System.out.println("上传文件名字:"+fileName); String suffix = fileName.substring(fileName.lastIndexOf('.'));//获取文件范例 String newFileName = bNo+"_"+bName+suffix; System.out.println(newFileName); //获取输入流,个中有上传文件的内容 InputStream is = item.getInputStream(); //String path = this.getServletContext().getRealPath("/net/bookImage");//获恰当前项目生存处事器地点,也就是web文件夹下 String path ="D:\\tomcat\\apache-tomcat-9.0.35-windows-x64\\apache-tomcat-9.0.35\\webapps\\librarySystem\\web\\net\\bookImage"; //文件夹内文件数目有上限,可是可以建设子目次 //获取当前系统时间 Calendar now = Calendar.getInstance(); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH)+1; int day = now.get(Calendar.DAY_OF_MONTH); path = path+"https://www.jb51.net/"+year+"https://www.jb51.net/"+month+"https://www.jb51.net/"+day; //若该目次不存在,直接建设新目次 File dirFile = new File(path); if(!dirFile.exists()){ dirFile.mkdirs(); } //建设方针文件,用来生存上传文件 File desFile = new File(path,newFileName); //建设文件输出流 OutputStream os = new FileOutputStream(desFile); //将输入流数据写入到输出流中 int len=-1; byte[]buf = new byte[1024]; while((len=is.read(buf))!=-1){ os.write(buf,0,len); } //desFile.delete();//删除姑且文件 os.close();//输出流 is.close();//输入流 //删除姑且文件 item.delete(); } } } catch (FileUploadException e) { e.printStackTrace(); } } }

总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsddjd.html