文件上传至服务器
2021-07-05 21:03
标签:0kb web bsp block string 用户 路径 port private 使用文件上传至服务器需要导入两包commons-fileupload-1.2.2.jar与commons-io-2.4.jar 本文的servlet使用的是 servlet3.0注解配置, 不用写web.xml 文件了 建立文件时使用javaEE6.0 支持servlet3.0 value的值就是访问路径 文件上传的表单 method 必须是post 例如 创建表单的步骤 1.创建DiskFileItemFactory 工厂 整体代码如下: 如果再加上 大小: 获取上传文件名的后缀: 完整的代码如下: 文件上传至服务器 标签:0kb web bsp block string 用户 路径 port private 原文地址:http://www.cnblogs.com/goodtimealways/p/7105775.html
urlPatterns的值也是访问路径 例如
@WebServlet(name="DemoServlet3",value="/demoServlet3")
public class DemoServlet3 extends HttpServlet {
...
}
enctype="multipart/form-data"
DiskFileItemFactory dif = new DiskFileItemFactory();
2.创建ServletFileUpload 对象
ServletFileUpload sfu = new ServletFileUpload(dif);
3.判断表单提交 是不是 multipart/form-data
boolean isMultipart = sfu.isMultipartContent(request);import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet(name="UploadServlet" ,urlPatterns="/uploadServlet")
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("------------文件上传----------------");
//文件上传路径 我的文件上传的是F:\tomcat7\webapps\javaweb下的新建file文件夹里
String readPath=this.getServletContext().getRealPath("file");
//创建DiskFileItemFactory工厂
DiskFileItemFactory dif=new DiskFileItemFactory();
ServletFileUpload sfu=new ServletFileUpload(dif);
boolean iMmultipart = sfu.isMultipartContent(request);
if(iMmultipart){
//进行文件上传
try {
//解析请求 获取表单元素即表单的全部提交信息
List
限制文件上传的类型和大小
类型:
List
//设置单个文件的最大限制
upload.setSizeMax(1024*30);
String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println("后缀:"+fileType);
boolean flag = filTypes.contains(fileType);package cn.bdqn.demo;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
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.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet(name="UploadServlet",value="/uploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = -2590255173244980573L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("----文件上传---------");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
//获取上传文件的路径
String realPath = this.getServletContext().getRealPath("files");
System.out.println("上传路径:"+realPath);
//1.创建DiskFileItemFactory
DiskFileItemFactory dif = new DiskFileItemFactory();
//2.创建ServletFileUpload 对象
ServletFileUpload sfu = new ServletFileUpload(dif);
//3.判断表单提交 是不是 multipart/form-data
boolean isMultipart = sfu.isMultipartContent(request);
if(isMultipart){
//进行文件上传
//ServletFileUpload 解析请求 获取元素集合
List
上一篇:python_logging模块