jsp文件的简单上传与下载

@[JSP文件的简单上传与下载]

  • 一、前期工具准备:eclipse(这里推荐eclipse,myeclipse也行,自己找了半天也找不到为什么无法使用组件的问题),tomcat,还有jspsmartupload组件的jar包。
    没有组件可以去网上百度下载,或者加扣扣:1367844194问我要。
  • 二、文件上传
    1、upload.html
<!DOCTYPE html>
<html>
  <head>
    <title>文件上传</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=GB2312">
    
    

  </head>
  
  <body>
   <p align="center">上传产品信息</p>
   <form method="post" action="do_upload.jsp" enctype="multipart/form-data">
   <table width="90%" border="1" align="center">
   	<tr>
   		<td><div align="center">产品图片:
   		<input type="file" name="filel" size="30">
   		</div></td>
   	</tr>
   	<tr>
   		<td><div align="center">产品说明:
   		<input type="file" name="filel" size="30">
   		</div></td>
   	</tr>
   	<tr>
   		<td><div align="center">
   		<input type="submit" name="submit" value="上传">
   		</div></td>
   	</tr>	
   </table>
   </form>
  </body>
</html>

2、do_upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*,com.jspsmart.upload.*" %>//导入上传文件所需组件的jar

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>文件上传处理页面</title>
   	<meta http-equiv="Content-Type" content="text/html;chatset=gb2312">

  </head>
  
  <body>
   <%
   		SmartUpload su = new SmartUpload();//创建SmartUpload对象
   		su.initialize(pageContext);//初始化页面,pageContext是内置对象
   		//su.setMaxFileSize(10000);
   		//su.setTotalMaxFileSize(20000);
   		//su.setAllowedFilesList("doc,txt");
   		//su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
   		su.upload();
   		int count = su.save("/upload",su.SAVE_VIRTUAL);
   		/*注意:upload是路径,此路径必须是tomcat中的文件路路径 如果没有要新建 ,新建在和web-inf同级文件夹中*/
   		out.println(count+"个文件上传成功!<br>");
   		for(int i=0;i<su.getFiles().getCount();i++)//遍历文件
   		{
   			com.jspsmart.upload.File file = su.getFiles().getFile(i);
   			if(file.isMissing()) continue;
   			out.println("<br>文件名:"+file.getFileName()+"长度:"+file.getSize());
   		}
    %>
  </body>
</html>

  • 三、文件下载
    1、download.html
<!DOCTYPE html>
<html>
  <head>
    <title>文件下载</title>
	<http-equiv="content-type"  content="text/html;charset=GB2312">
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=GB2312">
    
   
  </head>
  
  <body>
   	<p align="center">下载文件页面</p>
   	<form action="do_download.jsp" method="post" enctype="multipart/form-data">
   		<table width="75%" border="1" align="center">
   			<tr>
   				<td>
   					<div align="center">单击下载:
   					<a href="upload/shop.docx">电子商城使用说明书</a>
   					<input type="submit" name="download" value="下载">
   					</div>
   				</td>
   			</tr>
   		</table>
   	</form>
  </body>
</html>



2、do_download.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*,com.jspsmart.upload.*" %>

<!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>
  <%
  	SmartUpload su = new SmartUpload();
  	su.initialize(pageContext);
  	su.setContentDisposition(null);
  	su.downloadFile("upload/shop.docx");
   %>
  </body>
</html>

代码重要核心都加了注释!还有不懂得可以加我扣扣:1367844194,一起交流。

猜你喜欢

转载自blog.csdn.net/qq_43234774/article/details/84823890