廢話不多說,直接貼代嗎
頁面代碼:
<%@ 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>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>
${msg }
上傳
<form action="<%=basePath %>/SmartServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">
<input type="file" name="file4">
<input type="submit" value="上傳">
</form>
<hr/>
批量下載
<form action="<%=basePath%>/SmartDownServlet" method="get">
<input type="checkbox" name="filename" value="img1.jpg">Image2
<input type="checkbox" name="filename" value="img2.jpg">Image3
<input type="checkbox" name="filename" value="img3.jpg">Image4
<input type="submit" value="下載">
</form>
</body>
</html>
批量上傳,servlet代碼,需要注意的是需要將表單設置爲enctype=”multipart/form-data”,這樣就會以二進制流的形式傳輸表單
package com.leige.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;
public class SmartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path=this.getServletContext().getRealPath("/img");
System.out.println(path);
SmartUpload smartUpload=new SmartUpload();
//初始化對象
smartUpload.initialize(getServletConfig(), request, response);
//設置上傳文件大小
smartUpload.setMaxFileSize(1024*1024*10);
//設置所有文件的大小
smartUpload.setTotalMaxFileSize(1024*1024*100);
//設置允許上傳文件類型
smartUpload.setAllowedFilesList("txt,jpg,gif,png");
String result = "上傳成功!";
try {
//設置禁止上傳的文件類型
smartUpload.setDeniedFilesList("rar,jsp,js");
//上傳文件
smartUpload.upload();
int count = smartUpload.save(path);
System.out.println("上傳成功" + count + "個文件!");
} catch (Exception e) {
//smartupload定義的一些錯誤代碼,挑出常用的
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();
}
request.setAttribute("msg", result);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
批量下載,需要注意的是,當用戶選擇批量下載時,我們不可能每個文件都彈出一個下載框,這樣在體驗上也不好,而且大量數據不經過壓縮,就傳輸,也是對帶寬的浪費,所以好的解決辦法,壓縮成包:
package com.leige.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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;
import org.apache.commons.io.IOUtils;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class SmartDownServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
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("/") + "img/";
//初始化smartupload
SmartUpload sm=new SmartUpload();
sm.initialize(getServletConfig(), request, response);
sm.setContentDisposition(null);
String[] fileNames=request.getParameterValues("filename");
String str = "";
String rt = "\r\n";
//返回一個壓縮包
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
for(String filename : fileNames){
str += filename + rt;
File file = new File(path + filename);
zos.putNextEntry(new ZipEntry(filename));
FileInputStream fis = new FileInputStream(file);
//複製文件到壓縮流中
IOUtils.copy(fis, zos);
zos.flush();
fis.close();
}
//設置註釋
zos.setComment("下載成功:" + rt + str);
zos.flush();
zos.close();
}
}
下載和上傳使用非常廣泛,希望大家能掌握住