今日群里一兄弟问我能否给一份struts文件上传下载的例子,因为自己项目比较紧所以想在网上找
些源码给他,但是纵观全网,写的都不是太全,这让新手使用都不是太方便,利用周天花了30分钟写了
一个。发布出来,发布出来大家共同学习!其中包括数据库的设计,路径存储,重现上传删除服务器源
文件,图片预览,等!
知识你我共同分享!
首先明白文件上传下载的原理。
①、利用输入流个输出流InputStream OutputStream
②、我们的Struts自带的FileUtiles.copyFile上传组件的!
本文采用的事第二种!另为了方便读者可以直接的使用本源码,采取的事多文件上传。一劳永逸!另由于时间关系,采用存储方式是JDBC和Mysql
第一步:我们需要相关的开发架包:
如果这几个的意思您不懂可以直接百度。也可以给我留言!
第二步:建立我的数据库表结构
第三步:编写我们的DBUtil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package
org.usc.util;
import
java.sql.*;
public
class
DBUtil {
public
static
Connection conn =
null
;
public
static
Connection getConn() {
try
{
Class.forName(
"com.mysql.jdbc.Driver"
);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
try
{
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/oracle"
,
"root"
,
"root"
);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
conn;
}
public
static
void
close() {
if
(conn !=
null
) {
try
{
conn.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
}
}
public
static
void
main(String[] args) {
getConn();
System.out.println(getConn());
}
}
|
第四步:编写实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package
org.usc.entity;
/**
* 文件类,需要的时候,可以和数据库进行关联
*
*/
public
class
UploadFiles
{
private
String uploadFileName;
//上传的文件名称
private
String uploadContentType;
//类型
private
String uploadRealName;
//服务器保存的文件真实名称,UUID
private
String txt;
private
String path;
//如果使用数据库的话,建议这三个字段都进行保存
public
String getUploadFileName()
{
return
uploadFileName;
}
public
void
setUploadFileName(String uploadFileName)
{
this
.uploadFileName = uploadFileName;
}
public
String getUploadContentType()
{
return
uploadContentType;
}
public
void
setUploadContentType(String uploadContentType)
{
this
.uploadContentType = uploadContentType;
}
public
String getUploadRealName()
{
return
uploadRealName;
}
public
void
setUploadRealName(String uploadRealName)
{
this
.uploadRealName = uploadRealName;
}
public
String getTxt() {
return
txt;
}
public
void
setTxt(String txt) {
this
.txt = txt;
}
public
String getPath() {
return
path;
}
public
void
setPath(String path) {
this
.path = path;
}
}
|
第五步:编写相关Dao方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package
org.usc.dao;
import
java.sql.*;
import
org.usc.entity.UploadFiles;
import
org.usc.util.DBUtil;
public
class
Dao {
public
boolean
add(UploadFiles uploadFiles ){
boolean
result=
false
;
Connection conn=DBUtil.getConn();
try
{
PreparedStatement ps=conn.prepareStatement(
"insert into UploadFiles(uploadFileName,uploadRealName,uploadContentType,txt,path)values(?,?,?,?,?)"
);
ps.setString(
1
, uploadFiles.getUploadFileName());
ps.setString(
2
, uploadFiles.getUploadRealName());
ps.setString(
3
, uploadFiles.getUploadContentType());
ps.setString(
4
, uploadFiles.getTxt());
ps.setString(
5
, uploadFiles.getPath());
ps.execute();
result=
true
;
}
catch
(SQLException e) {
e.printStackTrace();
}
finally
{
DBUtil.close();
}
return
result;
}
// 根据ID的编号查询
public
UploadFiles querykById(
int
id) {
Connection conn = DBUtil.getConn();
UploadFiles uploadFiles =
new
UploadFiles();
try
{
PreparedStatement ps = conn
.prepareStatement(
"select*from UploadFiles where id = ?"
);
ps.setInt(
1
, id);
ResultSet rs = ps.executeQuery();
if
(rs.next()) {
uploadFiles.setPath(rs.getString(
"path"
));
uploadFiles.setTxt(rs.getString(
"txt"
));
uploadFiles.setUploadFileName(rs.getString(
"uploadFileName"
));
uploadFiles.setUploadRealName(rs.getString(
"uploadRealName"
));
uploadFiles.setUploadContentType(rs.getString(
"uploadContentType"
));
}
}
catch
(SQLException e) {
e.printStackTrace();
}
return
uploadFiles;
}
}
|
第六步:编写上传Action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package
org.usc.action;
import
java.io.File;
import
java.util.*;
import
org.apache.commons.io.FileUtils;
import
org.apache.struts2.ServletActionContext;
import
org.usc.dao.Dao;
import
org.usc.entity.UploadFiles;
import
com.opensymphony.xwork2.ActionSupport;
public
class
UploadAction
extends
ActionSupport {
/**
*
*/
private
static
final
long
serialVersionUID = 1L;
private
File[] upload;
// 实际上传文件
private
String[] uploadContentType;
// 文件的内容类型
private
String[] uploadFileName;
// 上传文件名
// 注意FileName和ContentType 必须这样写!以你的<input type="file" name="upload">
private
List<UploadFiles> uploadFiles =
new
ArrayList<UploadFiles>();
// 上传文件集合
private
String message;
public
static
String FILE_ROOT =
""
;
public
static
String UPLOAD_PATH =
"/upload"
;
// 上传文件路径
public
String execute()
throws
Exception {
try
{
String path = ServletActionContext.getServletContext().getRealPath(
FILE_ROOT);
String path2 = UPLOAD_PATH +
"/shangchuan/"
;
String targetDirectory = path + path2;
File file =
new
File(targetDirectory);
// 获取文件流路径
if
(!file.exists()) {
file.mkdirs();
}
for
(
int
i =
0
; i < upload.length; i++) {
String fileName = uploadFileName[i];
// 上传的文件名
String type = uploadContentType[i];
// 文件类型
String realName = UUID.randomUUID().toString()
+ getExt(fileName);
// 保存的文件名称,使用UUID+后缀进行保存
File target =
new
File(targetDirectory, realName);
FileUtils.copyFile(upload[i], target);
// 上传至服务器的目录,一般都这样操作,
/*
* UploadFiles uf = new UploadFiles();// 创建文件
* uf.setUploadContentType(type);
* uf.setUploadFileName(fileName);//上传的文件名称
* uf.setUploadRealName(realName);//保存数据库的文件名称 采用加密形式
* uploadFiles.add(uf);// 添加到需要下载文件的List集合中
*/// uploadRealName[i]=UUID.randomUUID().toString();
// System.out.println("uploadRealName:"+uploadRealName[i]);
/*
* for(File file:upload){
* System.out.println("filename:"+file.getName()); }
*/
System.out.println(
"filename真实的名字:"
+ fileName);
System.out.println(
"保存数据库中的加密名字"
+ realName);
System.out.println(
"Ext-后缀名:"
+ getExt(fileName));
System.out.println(
"Type-类型:"
+ type);
System.out.println(
"上传路径:"
+ path2 + realName);
System.out.println(
"----------------------------"
);
// 下面我们将相关的信息保存到数据库中去
UploadFiles uploadFiles =
new
UploadFiles();
uploadFiles.setPath(path2 + realName);
uploadFiles.setTxt(getExt(fileName));
uploadFiles.setUploadContentType(type);
uploadFiles.setUploadFileName(fileName);
uploadFiles.setUploadRealName(realName);
Dao dao =
new
Dao();
dao.add(uploadFiles);
}
// 通过dao方法根据ID值查出所对应对象的属性,在页面显示出来
Dao dao =
new
Dao();
UploadFiles ed = dao.querykById(
109
);
// 这是我测试用的id值 您的Id初始值应该为1
uploadFiles.add(ed);
System.out
.println(uploadFiles.get(
0
).getUploadFileName() +
">>>>>"
);
}
catch
(Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
}
return
SUCCESS;
}
// 本方法是截取后缀名的 不会的话百度搜索IndexOf方法
public
static
String getExt(String fileName) {
return
fileName.substring(fileName.lastIndexOf(
"."
));
}
//下面是get set方法
public
File[] getUpload() {
return
upload;
|