Java实现模拟QQ空间图片上传
首先看效果:
首先编写我们的上传jsp代码,如下:
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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>Java实现开发QQ空间上传</
title
>
<
style
type
=
"text/css"
>
body{background: url("image/bg_webp.jpg");}
*{padding: 0px;margin: 0px;}
.qq{width: 520px;height: 70px;margin: 200px auto;border: 1px solid #e6e6e6;background-color: #fff}
.qq .qq_pic{float: right;width: 48px;height: 70px;border-left: 1px solid #eee8d1;background-color: #fafafa;border-right: 1px solid #eee8d1 }
.qq .qq_pic .bg_button{width: 48px;height: 70px;display: block;padding: 20px 0px 0px 10px}
.qq .qq_pic .bg_button:HOVER{background-color: #fff;height: 50px;width: 38px;}
.qq .qq_pic .bg_button i{background: url("image/bg_button.png");background-position: -490px -125px;width: 25px;height: 25px;display: block;}
.qq .qq_pic input{display: none;}
</
style
>
</
head
>
<
body
>
<
form
action="<%=request.getContextPath()%>/UploadQQServlet" enctype="multipart/form-data" method="post" id="saveForm">
<
div
class
=
"qq"
>
<
div
class
=
"qq_pic"
>
<
a
href
=
"#"
class
=
"bg_button"
onclick
=
"openBrowser()"
>
<
i
></
i
>
</
a
>
<
input
type
=
"file"
id
=
"fileName_upload"
name
=
"file1"
onchange
=
"saveFile()"
>
</
div
>
</
div
>
</
form
>
<
div
>
<
img
alt
=
"图片预览"
src
=
"upload/${fileName}"
style
=
"height: 30px;width: 30px;"
>
</
div
>
<
script
type
=
"text/javascript"
>
function openBrowser(){
var boolean=navigator.appName=="Microsoft Internet Explorer"?true:false;
if(boolean){
document.getElementById("fileName_upload").click();
}else{
var a=document.createEvent("MouseEvents");
a.initEvent("click",true,true);
document.getElementById("fileName_upload").dispatchEvent(a);
}
}
function saveFile(){
document.getElementById("saveForm").submit();
}
</
script
>
</
body
>
</
html
>
|
然后编写我们的后台代码,这里我就简单使用Servlet
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
|
package
com.wonders.upload;
import
java.io.File;
import
java.io.IOException;
import
java.util.Iterator;
import
java.util.List;
import
java.util.UUID;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.commons.fileupload.FileItem;
import
org.apache.commons.fileupload.FileItemFactory;
import
org.apache.commons.fileupload.FileUploadException;
import
org.apache.commons.fileupload.disk.DiskFileItemFactory;
import
org.apache.commons.fileupload.servlet.ServletFileUpload;
public
class
UploadQQServlet
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 {
request.setCharacterEncoding(
"utf-8"
);
response.setCharacterEncoding(
"utf-8"
);
//获取文件路径
String strPath=request.getRealPath(
"/"
)+
"/upload"
;
File file =
new
File(strPath);
if
(!file.exists())file.mkdirs();
FileItemFactory factory=
new
DiskFileItemFactory();
ServletFileUpload upload=
new
ServletFileUpload(factory);
//从请求对象中获取文件信息
List items;
try
{
items = upload.parseRequest(request);
if
(items!=
null
){
for
(
int
i=
0
;i<items.size();i++){
Iterator iterator=items.iterator();
while
(iterator.hasNext()){
FileItem item=(FileItem)iterator.next();
if
(item.isFormField()){
continue
;
}
else
{
String fileName=item.getName();
Long fileSize=item.getSize();
int
pos=fileName.indexOf(
"."
);
String ext=fileName.substring(pos,fileName.length());
fileName=UUID.randomUUID().toString()+ext;
request.getSession().setAttribute(
"fileName"
, fileName);
File saveFile=
new
File(strPath,fileName);
item.write(saveFile);
response.sendRedirect(
"UploadQQ.jsp"
);
}
}
}
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
ok,基本完成,此代码练手而用!
本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1608855,如需转载请自行联系原作者