java文件上传Demo(必看篇)
程序员文章站
2024-02-21 22:59:10
说到文件上传我们要做到:
1.引入两个包:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar
2.将form改为上传文件...
说到文件上传我们要做到:
1.引入两个包:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar
2.将form改为上传文件模式:enctype="multipart/form-data"
3.开始编写相关代码
这里会用到几个关键的类:磁盘文件工厂diskfileitemfactory ; 创建servlet文件上传类:servletfileupload
还有几个重要的方法:diskfileitemfactory类用于将以临时文件形式保存在磁盘上的存放目录的方法setrepository;
servletfileupload类得到表单中所有的数据,得到form表单中所有的元素方法:parserequest
下面看具体代码:
说明以这种方式上传文件是保存在服务器端的!
import java.io.file; import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import java.util.list; import java.util.uuid; import javax.servlet.servletexception; 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; import org.apache.commons.io.fileutils; public class uploadservlet extends httpservlet { /** * constructor of the object. */ public uploadservlet() { super(); } /** * destruction of the servlet. <br> */ public void destroy() { super.destroy(); // just puts "destroy" string in log // put your code here } /** * the doget method of the servlet. <br> * * this method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws servletexception if an error occurred * @throws ioexception if an error occurred */ public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } /** * the dopost method of the servlet. <br> * * this method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws servletexception if an error occurred * @throws ioexception if an error occurred */ public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); printwriter out = response.getwriter(); diskfileitemfactory sf= new diskfileitemfactory();//实例化磁盘被文件列表工厂 string path = request.getrealpath("/upload");//得到上传文件的存放目录 sf.setrepository(new file(path));//设置文件存放目录 sf.setsizethreshold(1024*1024);//设置文件上传小于1m放在内存中 string rename = "";//文件新生成的文件名 string filename = "";//文件原名称 string name = "";//普通field字段 //从工厂得到servletupload文件上传类 servletfileupload sfu = new servletfileupload(sf); try { list<fileitem> lst = sfu.parserequest(request);//得到request中所有的元素 for (fileitem fileitem : lst) { if(fileitem.isformfield()){ if("name".equals(fileitem.getfieldname())){ name = fileitem.getstring("utf-8"); } }else{ //获得文件名称 filename = fileitem.getname(); filename = filename.substring(filename.lastindexof("\\")+1); string houzhui = filename.substring(filename.lastindexof(".")); rename = uuid.randomuuid()+houzhui; fileitem.write(new file(path, rename)); } } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("普通字段"+name); system.out.println("文件名称"+filename); system.out.println("修改后生成的文件名称"+rename); response.sendredirect("ok.jsp"); out.flush(); out.close(); } /** * initialization of the servlet. <br> * * @throws servletexception if an error occurs */ public void init() throws servletexception { // put your code here } }
index.jsp页面:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>文件测试界面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> </head> <body> <div align="center"> <form action="uploadservlet" enctype="multipart/form-data" method="post"> 名称:<input name="name" /> <br> 图片:<input name="img" type="file"/><br> <input type="submit" value="提交" /> <input type="reset" value="重置" /> </form> </div> </body> </html>
ok.jsp页面:
<body> <h1 align="center">上传文件成功!</h1> </body>
实现效果就不截图了,有需要自己那过去用!
以上这篇java文件上传demo(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。