欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Form表单上传文件(type="file")的使用

程序员文章站 2022-05-14 19:14:44
一,单个文件的上传 1.html/jsp页面 <%@ page language="java" contenttype="text/html; chars...

一,单个文件的上传

1.html/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>
<%
  string path=request.getcontextpath();
  request.setattribute("path", path);
%>
</head>
<body>
<form action="${path}/fileser" method="post" enctype="multipart/form-data">
  <table>
    <tr>
      <td>上传文件</td>
      <td> 
        <input type="file" name="filen" />
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" />
        <input type="reset" />
      </td>
    </tr>
  </table>
  </form>
</body>
</html>

2.过滤器解决中文乱码问题

package com.ser;
import java.io.ioexception;
import javax.servlet.filter;
import javax.servlet.filterchain;
import javax.servlet.filterconfig;
import javax.servlet.servletexception;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
public class filter01 implements filter
{
  @override
  public void destroy()
  {
  }
  @override
  public void dofilter(servletrequest arg0, servletresponse arg1,
      filterchain arg2) throws ioexception, servletexception
  {
    httpservletrequest request= (httpservletrequest)arg0;
    httpservletresponse response=(httpservletresponse)arg1;
    request.setcharacterencoding("utf-8");
    response.setcharacterencoding("utf-8");
    response.setcontenttype("text/html charset=utf-8");
    arg2.dofilter(request, response);
  }
  @override
  public void init(filterconfig arg0) throws servletexception
  {
  }
}

3.处理html/jsp

package com.ser;
import java.io.file;
import java.io.ioexception;
import javax.servlet.servletconfig;
import javax.servlet.servletexception;
import javax.servlet.annotation.multipartconfig;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.part;
/**
 * servlet implementation class fileser
 */
@webservlet("/fileser")
@multipartconfig(location="e://uploadfile",
    filesizethreshold=1024*1024*1000,
    maxfilesize=1024*1024*1000,
    maxrequestsize=1024*1024*1024
    )
public class fileser extends httpservlet {
  private static final long serialversionuid = 1l;
  private static final string location="e://uploadfile";
  /**
   * @see httpservlet#httpservlet()
   */
  public fileser() {
  }
  @override
  public void init(servletconfig config) throws servletexception
  {
    file file =new file("e://uploadfile");
    if(!file.exists()&& !file.isdirectory())
    {
      file.mkdir();
    }
  }
  /**
   * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
   */
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  }
  /**
   * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
   */
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    part part= request.getpart("filen");
    string filetype=part.getcontenttype();
    string fileheader=part.getheader("content-disposition");
    //string filename=part.getname();
    long size=part.getsize();
    system.out.println(part);
    system.out.println(filetype);
    system.out.println(fileheader);
    //system.out.println(filename);
    system.out.println(size);
    string filename=fileheader.substring(fileheader.indexof("filename=")+10, fileheader.lastindexof("\""));
    part.write(filename);    
    response.getwriter().print(filename+"上传成功");
  }
}

总结

以上所述是小编给大家介绍的form表单上传域(type="file")的使用(上传文件),希望对大家有所帮助