jsp实现上传下载的功能

<!--选择上传文件的界面,这个也是jsp页面-->

<%@ page language="java" contentType="text/html; charset=gb2312" %>
<html>

<head>
<title>文件上传页面</title>
</head>

<body>
<form action="upload_do.jsp" method="post" enctype="multipart/form-data">

<tr>
<td>文件名:
<input type="file" name="Text" size="20"></td></tr>

<tr><td>姓名:<input type="file"size="20"name="zhang"></td></tr>
<tr>
<td>
<input type="submit" value="上传" name="res">
</td>
</tr>

</form>
</body>
</html>

 

upload_do.jsp页面:

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="java.sql.*,java.io.*"%>
<%@ page import="java.text.*"%>

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>

<%
String path = request.getContextPath();
//request.getScheme()返回的协议名称.默认是http,request.getServerName()就是获取你的网站的域名,如果是在本地的话就是localhost,request.getServerPort()  是你应用使用的端口,比如8080或者80  等等

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<body>
<%
try
{

//首先把文件上传至服务器

  // SmartUpload mySmartUpload = new SmartUpload();
   mySmartUpload.initialize(pageContext);
   mySmartUpload.service(request,response);
   mySmartUpload.upload();
   mySmartUpload.getFiles().getFile(0).getFieldName();

   String fn=mySmartUpload.getFiles().getFile(0).getFileName();
  
   mySmartUpload.getFiles().getFile(1).getFieldName();
  
    String fn2=mySmartUpload.getFiles().getFile(1).getFileName();

   mySmartUpload.save("F:/temp/");
   out.print("恭喜您,文件上传到服务器成功……");
     
//存入数据库
  
   String filepath="F:/temp/"+fn+"";
   String filepath2="F:/temp/"+fn2+"";

   //Date date=new Date(); "java.util.*"和java.sql.*两个包里都有date类,所以必须指定
   java.util.Date date = new java.util.Date();

   String time="";
   SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");
   time=s.format(date);//DATE型转换为String类型

//连接数据库:
String sql="";
Statement stmt = null;
ResultSet rs = null;

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=ProjManagerPlatform";
Connection dbcon=DriverManager.getConnection(url, "sa", "100862");

stmt = dbcon.createStatement();
sql="insert into ProjManagerPlatform.dbo.PatentInfo (patentAB,rightBook)  values ('"+filepath+"','"+filepath2+"')";
stmt.executeUpdate(sql);

out.print("<p>");
out.print("恭喜您,文件信息保存到数据库成功……");
}

catch (Exception e)
{
      e.printStackTrace();
      out.print(e);
}

%>

</body>
</html>

<!--
  实现下载的页面
-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>下载</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<a href="
http://127.0.0.1:8080/FileUpLoad/do_download.jsp">点击下载</a>
</body>
</html>

 

 do_download.jsp页面

<%@ page contentType="text/html;charset=GB2312"
import="com.jspsmart.upload.*" %>
<%@ page import="java.sql.*"%>
<%
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection  conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaceName=updata", "sa", "100862");
Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
try{
ResultSet rs;
rs = stmt.executeQuery("select 路径 from updata.dbo.updata where id='1'");
while(rs.next()){
 String url=rs.getString("路径");
 
  // 新建一个SmartUpload对象
 SmartUpload su = new SmartUpload();
  // 初始化
 su.initialize(pageContext);
  // 设定contentDisposition为null以禁止浏览器自动打开文件,
  //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
  //doc时,浏览器将自动用word打开它。扩展名为pdf时,
  //浏览器将用acrobat打开。
 su.setContentDisposition(null);
  // 下载文件
 su.downloadFile(url);

 }
 rs.close();
 stmt.close();
   conn.close();
}catch(Exception e){
  out.println(e.getMessage());
 } 
%>

 

 

注释:用此方法实现jsp的上传下载功能,需要先下载一个jspSmartUpload.jar 包。放到tomacat中的lib文件夹下。

 

说明:若果有不明白的地方,请留言。我会尽快回复!谢谢!有需要jspSmartUpload.jar包的,请留下邮箱。


 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值