Java实现上传和下载上传下载学习 2007-09-15 17:36:45 阅读165 评论0 字号:大中小 订阅 .
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'UpLoad.jsp' starting page</title>
</head>
<body>
<form action="upLoad.do" enctype="MULTIPART/FORM-DATA" method=post name="fileForm">
请选择上传的文件:<br/>
<input type="file" name="filename1"/>
<br/>
<input type="file" name="filename2"/>
<br/>
<input type="file" name="filename3"/>
<br/>
<input type="file" name="filename3"/>
<br/>
<input type="submit" value="开始上传"/>
</form>
</body>
</html>
这个是实现多文件上传的jsp页面,UpLoad.jsp
下面是实现多文件上传的FileUpload.java文件:
package load.up;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class FileUpload {
/**
* 实现文件上传功能
* @param request HttpServletRequest
* @param tempPath 临时路径
* @param uploadPath 上传路径
* @param newFileName 新文件名称
* @return boolean 是否成功
*/
public boolean doFileUpload(HttpServletRequest request, String upLoadPath,int msgId) {
/** 成功上传フラグ */
boolean flag=true;
try {
/** 获取当前系统时间对象 */
Date date = new Date();
if (!new File(upLoadPath).isDirectory())
new File(upLoadPath).mkdirs();
// if (!new File(tempPath).isDirectory())
// new File(tempPath).mkdirs();
DiskFileUpload diskFUpload = new DiskFileUpload();
/** 设置Upload文件大小尺寸 */
diskFUpload.setSizeMax(5 * 1024 * 1024);
/** 设置缓冲区大小 */
diskFUpload.setSizeThreshold(4 * 1024);
// /** 设置临时存储路径 */
// diskFUpload.setRepositoryPath(tempPath);
/** 得到所有上传文件List */
List fileItems = diskFUpload.parseRequest(request);
/** 取得上传文件List迭代器 */
Iterator it = fileItems.iterator();
/** 遍历迭代器中的对象,进行对象处理 */
while (it.hasNext()) {
/** 取得单个文件对象 */
FileItem fi = (FileItem) it.next();
/** 获取上传文件文件名,包括文件路径 */
String filePathName = fi.getName();
String sysYear = date.getYear() + "";
String sysMonth = date.getMonth() + "";
String sysDay = date.getDay() + "";
String sysHours = date.getHours() + "";
String sysMin = date.getMinutes() + "";
String sysSec = date.getSeconds() + "";
long sys=System.currentTimeMillis();
/** 取当前系统时间 */
String sysTime = sysYear + sysMonth + sysDay + sysHours
+ sysMin + sysSec;
/** 获取上传文件原文件名 */
String fileOldName=filePathName.substring(filePathName.lastIndexOf("\\") + 1);
/** 获取文件名,包括扩展名 */
String fileName=sysTime+sys+fileOldName;
/** 上传文件服务器端保存路径 */
String url=upLoadPath +"\\";
/** 文件存在性check判断 */
if (fileName != null) {
/** 服务器上保存上传文件 */
fi.write(new File(url+fileName));
/** 保存上传文件信息,更新TBL */
this.insertFileMsg(msgId, url, fileName, fileOldName);
flag = true;
} else {
flag = false;
}
}
} catch (Exception e) {
flag=false;
}
return flag;
}
/**
* 更新TBL,保存上传文件相关信息
* @param msgId 用户ID
* @param fileUrl 服务器端上传文件保存路径
* @param fileName 服务器端保存上传文件的文件名
*/
private void insertFileMsg(int msgId,String fileUrl,String fileName,String fileOldName){
Connection conn=null;
/** 获取数据库链接Connection */
conn=this.getConn();
String sql = "insert into tfile(message_id,file_url,file_name,file_old_name) values("
+ msgId
+ ",'"
+ fileUrl.replace("\\", "/")
+ "','"
+ fileName
+ "','"
+ fileOldName + "')";
// StringBuffer sb = new StringBuffer();
// sb.append("insert into tfile(message_id,file_url) values(2)");
System.out.println(sql);
try {
Statement stmt=conn.createStatement();
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(msgId+"::"+fileUrl+"::"+fileName);
}
/**
* 获取数据库链接Connection
* @return 数据库链接Connection
*/
private Connection getConn(){
Connection conn=null;
String driver_class="com.mysql.jdbc.Driver";
String url="jdbc:mysql://192.168.1.19:3306/ctemdidb?useUnicode=true&characterEncoding=utf8";
String userName="root";
String password="root123";
try {
Class.forName(driver_class);
conn=DriverManager.getConnection(url,userName,password);
} catch (Exception e) {
if(conn!=null){
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
return conn;
}
}
红色部分就是提供上传的方法。
下面是实现FileDownload的javaclass文件
package load.down;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import load.FileMsg;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
public class FileDownload {
public List getFileMsg(int msgId) {
List fileMsglist = new ArrayList();
Connection conn = null;
conn = this.getConn();
String sql = "select file_url,file_name,file_old_name from tfile where message_id="
+ msgId;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
FileMsg fileMsg = new FileMsg();
int messageId=rs.getInt("message_id");
String fileUrl = rs.getString("file_url");
String fileName = rs.getString("file_name");
String fileOldName = rs.getString("file_old_name");
fileMsg.setMessageId(messageId);
fileMsg.setFileUrl(fileUrl);
fileMsg.setFileName(fileName);
fileMsg.setFileOldName(fileOldName);
fileMsglist.add(fileMsg);
}
} catch (SQLException e) {
e.printStackTrace();
}
return fileMsglist;
}
/**