使用smartUpload组件进行文件上传以及获取文本数据

如何在jsp中进行文件上传,要用到第三方jar包--smartUpload.jar,首先下载该jar包,然后拷贝到你工程的lib目录下,当然你也可以添加到tomcat的lib目录中。

当利用form表单进行文件上传的时候必须把给文件封装成二进制流,封装二进制流只需要在form表单中加上enctype="multipart/form-data",form表单提交时就以二进制方式提交。

1:smartUpload.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>smartupload.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body>
  <form action="smartupload_demo1.jsp" method="post" enctype="multipart/form-data">
    请选择你要上传的文件   <input type="file" name="file"/>
    <input type="submit" value="上传">
    </form>
  </body>
</html>
2:smartUpload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*"%>
<%
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 'smartupload_demo1.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>
   <%
       SmartUpload smart = new SmartUpload(); //初始化组件
       smart.initialize(pageContext);//上传操作
       smart.upload();//上传准备
       smart.save("upload");//上传文件保存的文件夹
       out.print("<p>上传完成</p>");
       
    %>
    
  </body>
</html>

当调用save方法的时候必须自己在根目录下建立一个文件夹进行文件的保存。注意在利用smartUpload时,必须引入包名下的资源,否则实例化该对象的时候会报找不到改类的错误。如图:


最后测试结果如下:

上传文件,选择20.jpg,最后在根目录下找到上传的图片文件

最后获取上传文件如下:

3:但是利用该种提交方式只是针对文件的上传,那要是文本数据该怎么办,当然smartUpload组件保重给我们提供了方法,可以利用smartUpload对象获取request对象然后在获取文本数据,测试代码如下:

1):smartUpload_demo2.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>smartUpload_demo2.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body>
   <form action="smartupload_demo2.jsp" method="post" enctype="multipart/form-data">
      姓名:<input type="text" name="name"><br/>
      照片:<input type="file" name="picture"><br/>
      <input type="submit" value="上传">
   </form>
  </body>
</html>

2):smartupload_demo2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

  </head>
 
  <body>
    <%
        SmartUpload smart = new SmartUpload();
        smart.initialize(pageContext);
        smart.upload();
        String name =smart.getRequest().getParameter("name");
           smart.save("upload");
        
        
     %>
     <h1>姓名:<%=name %></h1>
     <h1>通过request获取的用户名:<%=request.getParameter("name") %></h1>
  </body>
</html>
测试结果如下:







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值