侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

JSP实现快速上传文件的方法

Java  /  管理员 发布于 7年前   177

本文实例讲述了JSP实现快速上传文件的方法。分享给大家供大家参考。具体如下:

这里演示JSP不使用第三方库,实现快速上传文件的功能

1. FileUpload.java:

package FileUpload;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import javax.servlet.ServletInputStream;/** *  *//** * @author Qch *  */public class FileUpload{  ServletInputStream in=null;  String fpath="C://";  public FileUpload()  {    fpath="C://";    in=null;  }  public void setInputStream(ServletInputStream in)  {    this.in=in;  }  public void setFpath(String p)  {    this.fpath=p;  }  public String getFpath()  {    return fpath;  }  public String getParameter()  {     String r=null;    try    {      r=getParameter(in);    }    catch (Exception e)    {      e.printStackTrace();    }    return r;  }  public long getFileUpload()  {    long r=-1;    try    {      r=getFileUpload(in,fpath);    }    catch (Exception e)    {      e.printStackTrace();    }    return r;  }  public String getParameter(ServletInputStream in)// 只能按顺序提取      throws Exception  {    int l = 0;    byte[] b = new byte[1024];    l = in.readLine(b, 0, b.length);// 依次是读取属性的开始符、名称、属性值的类型、属性的值    String si = new String(b);    if (si.startsWith("----------------------------"))    {// 表示是从开始符开始读,否则应为刚读取文件后的一个属性,此时应少读一次      l = in.readLine(b, 0, b.length);    }    l = in.readLine(b, 0, b.length);    l = in.readLine(b, 0, b.length);    String value = new String(b, 0, l);    return value;  }  public long getFileUpload(ServletInputStream in, String fpath)// 需要提供输入流和存储路径      throws Exception  {    // out.println("文件信息:<br>");    long begin = System.currentTimeMillis();// 传送时间计时开始    int l = 0;    byte[] b = new byte[1024];    l = in.readLine(b, 0, b.length);    String sign = new String(b, 0, l);// eg.-----------------------------7d9dd29630a34    l = in.readLine(b, 0, b.length);    String info = new String(b, 0, l);// eg.Content-Disposition:form-data;    // name="file";    l = in.readLine(b, 0, b.length);    // String type=new    // String(b,0,l);//eg.Content-Type:application/octet-stream(程序文件)    l = in.readLine(b, 0, b.length);    // String nulll=new String(b,0,l);//此值应为空    int nIndex = info.toLowerCase().indexOf("filename=\"");    int nLastIndex = info.toLowerCase().indexOf("\"", nIndex + 10);    String filepath = info.substring(nIndex + 10, nLastIndex);    int na = filepath.lastIndexOf("\\");    String filename = filepath.substring(na + 1);    // out.println("文件绝对路径:"+filepath+"<br>");    // out.println("文件名:"+filename+"<br><br>");    String path=fpath + filename;    File fi = new File(path);// 建立目标文件    if (!fi.exists()&&!fi.createNewFile())      return -2;    BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(        fi));    while ((l = in.readLine(b, 0, b.length)) > 0)    {      if (l == sign.length())      {        String sign1 = new String(b, 0, sign.length());        // out.println(sign1+"<br>");        if (sign1.startsWith(sign))// 比对是否文件已传完          break;      }      f.write(b, 0, l);      f.flush();    }    f.flush();    f.close();    long end = System.currentTimeMillis();// 传送时间计时结束    // out.println("上传文件用时:"+(end-begin)+"毫秒<br>");    return end - begin;  }}

2. submitFile.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="https:/article/<%=basePath%>">    <title>My JSP 'submitFile.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="https:/article/styles.css">  -->    <script language="javascript">      function check()      {        if (document.form2.name.value==""){          alert("姓名不能为空!!");          document.form2.name.focus();          return false;        }        if (document.form2.file.value==""){          alert("文件不能为空!!");          return false;        }        return true;      }    </script>  </head>  <body>    <br>    <form method="post" name="form2" enctype="MULTIPART/FORM-DATA"      action="AnswerFile.jsp">      <br>      <p align="center">                 <br>      </p>      <table width="530" border="1" bgcolor="#c0c0c0" align="center"        height="91">        <tbody>          <tr><td valign="top" align="right">  姓名  <br></td><td valign="top">  <input type="text" name="name"></td>          </tr>          <tr><td align="right">    文件</td><td align="left">     <input type="file" name="file"></td>          </tr>          <tr><td valign="top" align="right">  文件类型  <br></td><td valign="top" align="left">  <select size="1" name="leixing">    <option selected value="作业">      作业    </option>    <option value="课程设计">      课程设计    </option>    <option value="论文">      论文    </option>  </select></td>          </tr>          <tr><td align="right">  <input type="Submit" value="上传" name="button2" onclick="return(check());"></td><td align="left">     <input type="reset" value="重置" name="button3"></td>          </tr>        </tbody>      </table>      <p>                 <br>        <br>               </p>    </form>  </body></html>

3. AnswerFile.jsp:



<%@ page language="java" import="java.util.*,java.io.*"  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="https:/article/<%=basePath%>">    <title>My JSP 'AnswerFile.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="https:/article/styles.css">  -->  </head>  <body>    <jsp:useBean id="upload" scope="session" class="FileUpload.FileUpload"/>    <jsp:setProperty name="upload" value="C://" property="fpath"/>    <%      ServletInputStream in = request.getInputStream();      upload.setInputStream(in);      String nam = upload.getParameter();      out.println("姓名:" + nam + "<br><br>");      long time = upload.getFileUpload();      out.println("文件上传完毕,总共耗时:" + time + "毫秒<br>");      String leixing = upload.getParameter();      out.println("文件类型:" + leixing + "<br>");      in.close();    %>    <br>    <div align="right">      <a href="https:/article/index.jsp">回到首页</a>    </div>  </body></html>

希望本文所述对大家的JSP程序设计有所帮助。


  • 上一条:
    JSP上传excel及excel插入至数据库的方法
    下一条:
    JSP文件下载功能的4种方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在java中实现的脱敏工具类代码示例分享(0个评论)
    • zookeeper安装流程步骤(0个评论)
    • 在java中你背的“八股文”可能已经过时了(2个评论)
    • 在php8.0+版本中使用属性来增加值代码示例(3个评论)
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2018-03
    • 2020-03
    • 2023-05
    • 2023-11
    • 2024-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客