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")的使用(上传文件),希望对大家有所帮助
上一篇: vue修改vue项目运行端口号的方法
下一篇: 详解Vue微信公众号开发踩坑全记录
推荐阅读
-
vue中使用input[type="file"]实现文件上传功能
-
HTML5 form标签之解放表单验证、增加文件上传、集成拖放的使用方法
-
php使用curl模拟浏览器表单上传文件或者图片的方法
-
Spring Cloud下使用Feign Form实现微服务之间的文件上传
-
JavaScript实现form表单的多文件上传
-
Form表单上传文件(type="file")的使用
-
Ajax提交Form表单及文件上传的实例代码
-
vue中使用input[type="file"]实现文件上传功能
-
ajax form表单提交 input file中的文件
-
使用隐藏的iframe 隐藏form提交,仿AJax无刷新提交,可以实现无刷新上传文件