编辑jsp页面获取文件

 <html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'upload.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 action="upload1" method="post" enctype="Multipart/form-data">
file:<input type="file" name="upload">
<input type="submit" value="上传">
</form>
</body>
</html>

servlet上传:

 import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID; 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 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;
@WebServlet("/upload1") public class Upload1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 1、创建文件项工厂
*/ DiskFileItemFactory factory = new DiskFileItemFactory();
/**
* 2、创建解析请求 数据的ServletFileUpload对象
*/
ServletFileUpload upload = new ServletFileUpload(factory);
try {
/**
* 3、解析请求数据 返回FileItem 列表
*/
List<FileItem> list = upload.parseRequest(request);
/**
* 4、解析获取每一个FileItem 对象
*/ FileItem item = list.get(0); //验证当前FileItem 是否是表单字段 如果fales 则取到的是文件
item.isFormField();
/**
* 5、文件名及路径处理
*/
//处理文件
String filename= item.getName();
//截取文件扩展名
String extName = filename.substring(filename.lastIndexOf("."));
//生成UUID作为文件名
String newName= UUID.randomUUID().toString();
//获取服务器上自定义的存放文件的目录
String rootPath = request.getSession().getServletContext().getRealPath("/upload");
//生成完整的文件路径
String newPath = rootPath+"/"+newName+extName;
System.out.println(newPath);
/**
* 6、文件写入
*/
item.write(new File(newPath)); } catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} }

commons.fileupload 文件上传的更多相关文章

  1. Commons FileUpload文件上传组件

    Java实现的文件上传组件有好几种,其中最为“官方”的要数Apache Commons库中的FileUpload了吧. 页面 <form method="POST" enct ...

  2. 对FileUpload文件上传控件的一些使用方法说明

    //创建时间:2014-03-12 //创建人:幽林孤狼 //说明:FileUpload文件上传控件使用说明(只是部分)已共享学习为主 //可以上传图片,txt文档.doc,wps,还有音频文件,视屏 ...

  3. apache fileupload 文件上传,及文件进度设置获取

    文件上传action处理: boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) ...

  4. FileUpload文件上传控件

    1.FileUpload控件的主要功能是向指定目录上传文件.FileUpload控件不会自动上传控件,而需要设置相关的事件处理程序,然后在程序中实现文件上传. 2.FileUpload控件常见的属性 ...

  5. ASP.NET中的FileUpload文件上传控件的使用

    本篇文章教大家如何将客户端的图片或者文件上传到服务器: 无论是上传图片(.jpg .png .gif等等) 文档(word excel ppt 等等). 第一步:放入以下三个控件 Image控件,Fi ...

  6. Bootstrap Fileupload 文件上传

    1.在jsp中引入css与js文件, <link href="${ctx}/plugins/fileup/css/fileinput.css" media="all ...

  7. 改变FileUpload文件上传控件的显示方式,确认后上传

    一.Aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="File ...

  8. 改变FileUpload文件上传控件的显示方式,选择文件后自动上传

    一.Aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="File ...

  9. 转:在Struts 2中实现文件上传

    (本文转自:http://www.blogjava.net/max/archive/2007/03/21/105124.html) 前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题 ...

随机推荐

  1. thinkphp 5.0整合阿里大于验证码短信发送接口,含完整模型验证实例DEMO

    为大家分享一个阿里大于短信发送接口: 首先创建一个发送模型(Send.php): <?php namespace app\index\model; use think\Validate; cla ...

  2. Go:错误处理

    在默认情况下,当程序发生错误(panic)后,程序就会退出(崩溃),所以我们希望,当程序发生错误后,可以捕获到错误,并进行处理,保证程序可以继续执行.比如捕获到错误后,打入日志或给管理员一个提示(邮件 ...

  3. PHP:车牌照合法性

    文章来源:http://www.cnblogs.com/hello-tl/p/7592362.html <?php /** * [verifyCarCard description] 效验车牌号 ...

  4. STM32——NVIV:嵌套中断向量控制器

    STM32有43个channel的settable的中断源:AIRC(Application Interrupt and Reset Register)寄存器中有用于指定优先级的4 bits.这4个b ...

  5. 安装 asp.net core 出错

    I received the same error message on a fresh Windows 10 install, with a fresh Visual Studio 2015 ins ...

  6. STM32F407 IO引脚复用器和映射 个人笔记

    基本概念 stm32有一些内置外设,每个外设有一个复用功能AF(Alternate functions). stm32的每个io引脚东路有一个16路复用器,该复用器一端连该引脚,另外16端连AF0~A ...

  7. Automation 的 ReportFlow

    ReportFlow: // click the Grid icon and switch to grid page public void changeToGrid() // click the A ...

  8. Quartz进一步学习与使用

    一.再思考 了解Quartz.NET的基本使用方法了.但如果想方便的知道某个作业执行情况,需要暂停,启动等操作行为,这时候就需要个Job管理的界面,如何才能达到我们想到的效果,查看相关Quartz.n ...

  9. onclick方法和$("").click()有不一样的地方

    话说是这样的...昨天写了一个文件上传的功能,是这样的,用fastdfs上传成功后会有一个url... 然后我自己测试上传,包括在文件服务器里都能找到.. 然后就自己打包发版了,都很正常也没报错... ...

  10. JDBC中的批处理

    以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/batch-processing.html: 批处理是指将关联的SQL语句组合成一个批处理,并将他们当成 ...