萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> jsp編程 >> java使用smartupload組件實現文件上傳的方法

java使用smartupload組件實現文件上傳的方法

 這篇文章主要介紹了java使用smartupload組件實現文件上傳的方法,對比分析了使用組件與不使用組件實現文件上傳的區別,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了java使用smartupload組件實現文件上傳的方法。分享給大家供大家參考。具體分析如下:

文件上傳幾乎是所有網站都具有的功能,用戶可以將文件上傳到服務器的指定文件夾中,也可以保存在數據庫中,這裡主要說明smartupload組件上傳。

在講解smartupload上傳前,我們先來看看不使用組件是怎麼完成上傳的原理的?

廢話不多說直接上代碼:

代碼如下: import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadTools {
private HttpServletRequest request = null; // 取得HttpServletRequest對象
private List<FileItem> items = null; // 保存全部的上傳內容
private Map<String, List<String>> params = new HashMap<String, List<String>>(); // 保存所有的參數
private Map<String, FileItem> files = new HashMap<String, FileItem>();
private int maxSize = 3145728; // 默認的上傳文件大小為3MB,3 * 1024 * 1024
public FileUploadTools(HttpServletRequest request, int maxSize,
String tempDir) throws Exception { // 傳遞request對象、最大上傳限制、臨時保存目錄
this.request = request; // 接收request對象
DiskFileItemFactory factory = new DiskFileItemFactory(); // 創建磁盤工廠
if (tempDir != null) { // 判斷是否需要進行臨時上傳目錄
factory.setRepository(new File(tempDir)); // 設置臨時文件保存目錄
}
ServletFileUpload upload = new ServletFileUpload(factory); // 創建處理工具
if (maxSize > 0) { // 如果給的上傳大小限制大於0,則使用新的設置
this.maxSize = maxSize;
}
upload.setFileSizeMax(this.maxSize); // 設置最大上傳大小為3MB,3 * 1024 * 1024
try {
this.items = upload.parseRequest(request);// 接收全部內容
} catch (FileUploadException e) {
throw e; // 向上拋出異常
}
this.init(); // 進行初始化操作
}
private void init() { // 初始化參數,區分普通參數或上傳文件
Iterator<FileItem> iter = this.items.iterator();
IPTimeStamp its = new IPTimeStamp(this.request.getRemoteAddr()) ;
while (iter.hasNext()) { // 依次取出每一個上傳項
FileItem item = iter.next(); // 取出每一個上傳的文件
if (item.isFormField()) { // 判斷是否是普通的文本參數
String name = item.getFieldName(); // 取得表單的名字
String value = item.getString(); // 取得表單的內容
List<String> temp = null; // 保存內容
if (this.params.containsKey(name)) { // 判斷內容是否已經存放
temp = this.params.get(name); // 如果存在則取出
} else { // 不存在
temp = new ArrayList<String>(); // 重新開辟List數組
}
temp.add(value); // 向List數組中設置內容
this.params.put(name, temp); // 向Map中增加內容
} else { // 判斷是否是file組件
String fileName = its.getIPTimeRand()
+ "." + item.getName().split(".")[1];
this.files.put(fileName, item); // 保存全部的上傳文件
}
}
}
public String getParameter(String name) { // 取得一個參數
String ret = null; // 保存返回內容
List<String> temp = this.params.get(name); // 從集合中取出內容
if (temp != null) { // 判斷是否可以根據key取出內容
ret = temp.get(0); // 取出裡面的內容
}
return ret;
}
public String[] getParameterValues(String name) { // 取得一組上傳內容
String ret[] = null; // 保存返回內容
List<String> temp = this.params.get(name); // 根據key取出內容
if (temp != null) { // 避免NullPointerException
ret = temp.toArray(new String[] {});// 將內容變為字符串數組
}
return ret; // 變為字符串數組
}
public Map<String, FileItem> getUploadFiles() {// 取得全部的上傳文件
return this.files; // 得到全部的上傳文件
}
public List<String> saveAll(String saveDir) throws IOException { // 保存全部文件,並返回文件名稱,所有異常拋出
List<String> names = new ArrayList<String>();
if (this.files.size() > 0) {
Set<String> keys = this.files.keySet(); // 取得全部的key
Iterator<String> iter = keys.iterator(); // 實例化Iterator對象
File saveFile = null; // 定義保存的文件
InputStream input = null; // 定義文件的輸入流,用於讀取源文件
OutputStream out = null; // 定義文件的輸出流,用於保存文件
while (iter.hasNext()) { // 循環取出每一個上傳文件
FileItem item = this.files.get(iter.next()); // 依次取出每一個文件
String fileName = new IPTimeStamp(this.request.getRemoteAddr())
.getIPTimeRand()
+ "." + item.getName().split(".")[1];
saveFile = new File(saveDir + fileName); // 重新拼湊出新的路徑
names.add(fileName); // 保存生成後的文件名稱
try {
input = item.getInputStream(); // 取得InputStream
out = new FileOutputStream(saveFile); // 定義輸出流保存文件
int temp = 0; // 接收每一個字節
while ((temp = input.read()) != -1) { // 依次讀取內容
out.write(temp); // 保存內容
}
} catch (IOException e) { // 捕獲異常
throw e; // 異常向上拋出
} finally { // 進行最終的關閉操作
try {
input.close(); // 關閉輸入流
out.close(); // 關閉輸出流
} catch (IOException e1) {
throw e1;
}
}
}
}
return names; // 返回生成後的文件名稱
}
}

 

上面代碼便可以完成無組件上傳。

下面開始講解smartupload

smartupload是由www.jspsmart.com網站開發的一套上傳組件包,可以輕松的實現文件的上傳及下載功能,smartupload組件使用簡單、可以輕松的實現上傳文件類型的限制、也可以輕易的取得上傳文件的名稱、後綴、大小等。

smartupload本身是一個系統提供的jar包(smartupload.jar),用戶直接將此包放到classpath下即可,也可以直接將此包拷貝到TOMCAT_HOMElib目錄之中。

下面使用組件完成上傳

單一文件上傳:

代碼如下: <html>
<head><title>smartupload組件上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<form action="smartupload_demo01.jsp" method="post" enctype="multipart/form-data">
圖片<input type="file" name="pic">
<input type="submit" value="上傳">
</form>
</body>
</html>

 

jsp代碼:

smartupload_demo01.jsp

代碼如下: <%@ page contentType="text/html" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<html>
<head><title>smartupload組件上傳01</title></head>

 

copyright © 萬盛學電腦網 all rights reserved