一、简介
SmartUpload一种java上传组件包,可以轻松的实现文件的上传及下载功能。
使用该组件可以轻松的实现上传文件的限制,也可以轻易的取得文件上传的名称、后缀、大小等。
二、详细介绍【百度百科有相对详细的介绍及使用】
三、具体实现例子【jsp+SmartUpload】
项目目录
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SmartUploadServlet</servlet-name>
<servlet-class>com.wuhn.smartupload.servlet.SmartUploadServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SmartDownloadServlet</servlet-name>
<servlet-class>com.wuhn.smartupload.servlet.SmartDownloadServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>BatchSmartDownloadServlet</servlet-name>
<servlet-class>com.wuhn.smartupload.servlet.BatchSmartDownloadServlet</servlet-class>
</servlet>
<!-- 上传servlet -->
<servlet-mapping>
<servlet-name>SmartUploadServlet</servlet-name>
<url-pattern>/SmartUploadServlet.do</url-pattern>
</servlet-mapping>
<!-- 下载servlet -->
<servlet-mapping>
<servlet-name>SmartDownloadServlet</servlet-name>
<url-pattern>/SmartDownloadServlet.do</url-pattern>
</servlet-mapping>
<!-- 批量下载servlet -->
<servlet-mapping>
<servlet-name>BatchSmartDownloadServlet</servlet-name>
<url-pattern>/BatchSmartDownloadServlet.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
SmartUploadServlet.java
package com.wuhn.smartupload.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
/**
* @author wuhn
* @创建时间 2015-12-08
* @功能 SmartUpload 上传
* **/
public class SmartUploadServlet extends HttpServlet {
/**
* The doGet method of the servlet.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);//默认post
}
/**
* The doPost method of the servlet.
*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置上传文件保存路径
String filePath = getServletContext().getRealPath("/")+"images";
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
SmartUpload smartUpload = new SmartUpload();
//初始化对象
smartUpload.initialize(getServletConfig(), request, response);
//设置上传文件
smartUpload.setMaxFileSize(1024*1024*10);
//设置所有文件的大小
smartUpload.setTotalMaxFileSize(1024*1024*100);
//设置文件的类型
smartUpload.setAllowedFilesList("txt,jpg,gif");
String result = "上传成功!";
//设置禁止上传的文件类型
try {
smartUpload.setDeniedFilesList("rar,jsp,js");
//上传文件
smartUpload.upload();
//保存文件
int count = smartUpload.save(filePath);
} catch (Exception e) {
//捕捉Exception异常 ,不然捕捉到异常
result = "上传失败!";
if(e.getMessage().indexOf("1015") != -1){
result = "上传失败:上传文件类型不正确!";
}else if (e.getMessage().indexOf("1010") != -1) {
result = "上传失败:上传文件类型不正确!";
}else if (e.getMessage().indexOf("1105") != -1) {
result = "上传失败:上传文件大小大于允许上传的最大值!";
}else if (e.getMessage().indexOf("1110") != -1) {
result = "上传失败:上传文件总大小大于允许上传总大小的最大值!";
}
e.printStackTrace();
}
//获取上传文件的属性
for(int i=0;i<smartUpload.getFiles().getCount();i++){
com.jspsmart.upload.File tempFile = smartUpload.getFiles().getFile(i);
System.out.println("***************");
System.out.println("表单中name的值:"+tempFile.getFileName());
System.out.println("上传文件名:"+tempFile.getFileName());
System.out.println("上传文件大小:"+tempFile.getSize());
System.out.println("上传文件的拓展名:"+tempFile.getFileExt());
System.out.println("上传文件全名:"+tempFile.getFilePathName());
System.out.println("***************");
}
System.out.println("上传结果:"+result);
request.setAttribute("result", result);
request.getRequestDispatcher("/jsp/01.jsp").forward(request, response);
}
}
SmartDownloadServlet.java
package com.wuhn.smartupload.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
/**
* @author wuhn
* @创建时间 2015-12-08
* @功能 SmartUpload 下载
* **/
public class SmartDownloadServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String result = "下载成功";
//获取下载的文件名
String filename = request.getParameter("filename");
//下载
SmartUpload smartUpload = new SmartUpload();
smartUpload.initialize(getServletConfig(), request, response);
smartUpload.setContentDisposition(null);//取消默认打开方式
try {
smartUpload.downloadFile("/images/"+filename);
} catch (Exception e) {
result = "下载失败";
System.out.println("********异常处理********");
if(e.getMessage().indexOf("系统找不到指定的路径。") != -1){
result = "下载失败:文件不存在!";
}
e.printStackTrace();
}
request.setAttribute("result", result);
request.getRequestDispatcher("/jsp/02.jsp").forward(request, response);
}
}
BatchSmartDownloadServlet.java
package com.wuhn.smartupload.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author wuhn
* @创建时间 2015-12-09
* @功能 SmartUpload 批量下载
* **/
public class BatchSmartDownloadServlet extends HttpServlet {
/**
* The doGet method of the servlet.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置下载相应信息
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename=test.zip");
//下载路径
String path = getServletContext().getRealPath("/")+"images/";
//获取下载的所有文件名
String[] filenames = request.getParameterValues("filename");
String str = "";
String rt = "\r\n";
//设置压缩信息
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
//获取需要下载的文件
for(String filename:filenames){
str += filename + rt;
File file = new File(path + filename);
zipOutputStream.putNextEntry(new ZipEntry(filename));//加入压缩文件
FileInputStream fileInputStream = new FileInputStream(file);
byte b[] = new byte[1024];
int n=0;
while ((n=fileInputStream.read(b)) != -1) {
zipOutputStream.write(b, 0, n);
}
zipOutputStream.flush();
fileInputStream.close();
}
zipOutputStream.setComment("download success:" + rt +str);//zip注释信息
zipOutputStream.flush();
zipOutputStream.close();
}
}
前台页面jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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>
<a target="_blank" href="<%=path%>/jsp/01.jsp">上传01</a>
<br>
<a target="_blank" href="<%=path%>/jsp/02.jsp">下载02</a>
<br>
<a target="_blank" href="<%=path%>/jsp/03.jsp">批量下载03</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>SmartUpload_批量上传</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="<%=path%>/SmartUploadServlet.do" method="post" enctype="multipart/form-data">
上传文件1:<input id="myfile1" name="myfile1" type="file"/><br>
上传文件2:<input id="myfile2" name="myfile2" type="file"/><br>
上传文件3:<input id="myfile3" name="myfile3" type="file"/>
<input type="submit" value="提交" /> ${result}
</form>
</body>
</html>
02.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '02.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>
下载:<a href="<%=path %>/SmartDownloadServlet.do?filename=jplin-css.jpg">文件</a> ${result}
</body>
</html>
03.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE>
<html>
<head>
<base href="<%=basePath%>">
<title>批量下载</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>
<h3>批量下载</h3>
<form action="<%=path %>/BatchSmartDownloadServlet.do" method="post">
<input type="checkbox" name="filename" value="jplugin-css.jpg"/>jplugin-css.jpg<br>
<input type="checkbox" name="filename" value="jplugin-multiple.jpg"/>jplugin-multiple.jpg<br>
<input type="checkbox" name="filename" value="jplugin-nocss.jpg"/>jplugin-nocss.jpg<br>
<input type="submit" value="提交">
</form>
</body>
</html>
四、项目代码【点击这里下载】