web开发中的文件上传
来源:互联网 发布:js弹出遮罩层 编辑:程序博客网 时间:2024/06/16 14:25
1:下载两个组件
FileUpload下载地址:
http://commons.apache.org/fileupload/
下载:commons-fileupload-1.2.2-bin.zip 得到:commons-fileupload-1.2.2.jar
http://commons.apache.org/io/
下载:commons-io-1.4-bin.zip 得到:commons-io-1.4.jar
2:新建一个jsp,名字为fileuplod.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'fileupload.jsp' starting page</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form method="post" action="fileupload" enctype="multipart/form-data"> 上传人: <input name="name" type="text" id="name" size="20" ><br> 上传文件: <input name="file" type="file" size="20" > <input type="submit" name="submit" value="uplode" > </form> </body></html>
3:新建一个servlet,名字为fileuplod
import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import javax.servlet.ServletConfig;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;public class fileupload extends HttpServlet { //上传到的路径 private String uploadPath="E://MyEclipse Professional 2014//Workspace//shop//WebRoot//admin//images//"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List items = upload.parseRequest(request); Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); //判断上传的是不是文件 if (item.isFormField()) { System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8")); } else { if (item.getName() != null && !item.getName().equals("")) { System.out.println("上传文件的大小:" + item.getSize()); System.out.println("上传文件的类型:" + item.getContentType()); System.out.println("上传文件的名称:" + item.getName()); File tempFile = new File(uploadPath+item.getName()); item.write(tempFile); System.out.println("上传文件成功!"); }else{ System.out.println("没有选择上传文件!"); } } } }catch(FileUploadException e){ e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.print("上传文件失败!"); } } }
0 0
- web开发中的文件上传
- Web开发中的文件上传问题研究
- Web开发--上传文件
- Web.py中的文件上传
- WEB中的文件上传01
- WEB中的上传文件02
- web应用开发-文件上传
- Java web 开发 文件上传讲解
- Web开发之文件的上传
- 移动Web开发之HTML5文件上传
- Tornado web开发之简单文件上传
- Spring Boot Web应用开发 文件上传
- 互联网开发--web文件上传原理
- 互联网开发-web文件上传性能问题
- web.config文件中的控制文件上传大小
- web 项目中的文件上传服务器的代码实现
- java通过struts实现web中的文件上传
- 详细阐述Web开发中的图片上传问题
- Mongod3.0 配置身份验证 db.createUser()
- 25. JavaScript PopupAlert
- HTTP协议中的GET和POST方法
- 阿里云添加swap
- 解决——Matlab中文显示乱码
- web开发中的文件上传
- 新手写模板函数
- Linux命令行编辑常见的快捷键(实用, 通用)
- ArcGIS engine C#开发出现未能加载程序集“ESRI.ArcGIS.Controls, Version=10.0.0.0, Culture=neutral, PublicKeyToken=
- offsetof(TYPE, MEMBER)解释
- NGUI Key Binding使用方法
- Xutils框架-HTTP源码详解
- 疯狂的创业运动
- Serv-U FTP文件服务器端口映射