大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在java项目中使用Demo如何实现一个文件上传功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
公司主营业务:做网站、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出青山免费做网站回馈大家。
说到文件上传我们要做到:
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.
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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.
* * 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 { Listlst = 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.
* * @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"%>文件测试界面
ok.jsp页面:
上传文件成功!
看完上述内容,你们掌握在java项目中使用Demo如何实现一个文件上传功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!