图片: <form:hidden path=
"picPath"
id=
"picPath"
></form:hidden>
<span id=
"${uuid}-statusPic"
style=
"color: #666;"
>
预览</span>
修改
<!-- 点击修改调出窗口 -->
<div id=
"${uuid}-uploadWindow"
class
=
"easyui-window"
title=
"图片上传"
modal=
"true"
resizable=
"false"
collapsible=
"false"
minimizable=
"false"
maximizable=
"false"
closed=
"true"
style=
"width:520px;height:100px;padding:5px;background: #fafafa;"
>
<div
class
=
"easyui-layout with iframe"
fit=
"true"
>
<div region=
"center"
border=
"false"
style=
"padding:10px;background:#fff;border:1px solid #ccc;"
>
<form action=
"menu/SmartMenu.do?action=uploadFile"
method=
"post"
enctype=
"multipart/form-data"
style=
"color: #666;"
id=
"${uuid}-tforma"
>
<input name=
"picPath"
id=
"${uuid}-picPath"
value=
"${command.picPath}"
type=
"hidden"
>
图片路径: <input name=
"itemPic"
alt=
"\" accept="
image/*
" id="
${uuid}-itemPic
" type="
file
">图片大小不超过2M<input class="
easyui-linkbutton
" value="
上传
" type="
submit">
</form>
</div>
</div>
</div>
<!-- 页面刚刚加载时调用 -->
init: function(uuid) {
// this.identifier 是设定的全局变量,uuid是页面加载时的唯一编码
this
.identifier = uuid;
// 图片上传
var idf =
this
.identifier;
var that =
this
;
$(
'#'
+idf+
'-tforma'
).ajaxForm({
dataType :
'json'
,
beforeSubmit : function(a, f, o) {
$(
'#'
+idf+
'-statusPic'
).html(
'上传中...'
);
},
success : function(data) {
if
(typeof (data) ==
'string'
)
data = eval(
'('
+ data +
')'
);
$(
'#'
+idf+
'-uploadWindow'
).window(
'close'
);
if
(
"success"
== data.message) {
$(
'div[identifier='
+that.identifier+
']'
).find(
'#picPath'
).val(data.path);
$(
"#"
+idf+
"-path"
).val(data.path);
$(
"#"
+idf+
"-statusPic"
).html(
"预览"
);
}
else
if
(
"error"
== data.message)
$(
"#"
+idf+
"-statusPic"
).html(
"非图片数据!"
);
else
$(
"#"
+idf+
"-statusPic"
).html(
"上传数据错误!"
);
$(
"#"
+idf+
"-itemPic"
).val(
''
);
},
error : function(jqXHR, textStatus,errorThrown) {
$(
'#$'
+idf+
'-uploadWindow'
).window(
'close'
);
//console.log("error:"+ data.responseText);
//console.log("status:" + textStatus);
$(
"#"
+idf+
"-statusPic"
).html(
"上传失败!"
);
$(
"#"
+idf+
"-itemPic"
).val(
''
);
} });
}
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/**
* <b>商品上传指定的图片</b>
*
* @param request
* @param response
* @param command
* @return
* @throws Exception
*/
public
ModelAndView uploadFile(HttpServletRequest request, HttpServletResponse response, SmartMenu command)
throws
Exception {
PrintWriter writer =
null
;
try
{
response.setContentType(
"application/json; charset=GBK"
);
writer = response.getWriter();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
String source = request.getSession().getServletContext().getRealPath(
"/"
);
// 获得第1张图片(根据前台的name名称得到上传的文件)
MultipartFile imgFile1 = multipartRequest.getFile(
"itemPic"
);
// 判断是否是重复上传图片(如果重复将删除旧图片)
String sourceImg = request.getParameter(
"path"
);
if
(imgFile1.getContentType().split(
"/"
)[
0
].equals(
"image"
)) {
if
(
null
!= sourceImg && !
""
.equals(sourceImg)) {
System.out.println(
"旧图片路径:"
+ sourceImg);
File f =
new
File(source + sourceImg);
if
(f.isFile()) {
f.delete();
System.out.println(
" 删除成功"
);
}
else
System.out.println(
" 删除失败!"
);
}
String path =
null
;
if
(imgFile1 !=
null
&& imgFile1.getSize() >
0
) {
path =
this
.getSmartMenuService().storeFile(request.getSession(), imgFile1);
}
writer.print(
"{\"message\":\"success\",\"path\":\""
+ path.replace(
"\\"
,
"\\\\"
) +
"\"}"
);
}
else
writer.print(
"{\"message\":\"error\"}"
);
}
catch
(Exception e) {
// TODO: handle exception
writer.print(
"{\"message\":\"no\"}"
);
}
writer.flush();
writer.close();
return
null
;
}
@Override
public
String storeFile(HttpSession session, MultipartFile file)
throws
Exception {
// TODO Auto-generated method stub
String fileType = file.getContentType().split(
"/"
)[
1
];
String path = session.getServletContext().getRealPath(
"/"
);
String separator = File.separator;
String uuid = UUID.randomUUID().toString();
FileOutputStream fos =
null
;
String fileName =
null
;
try
{
InputStream fis = file.getInputStream();
// 转换文件为png格式,并保存在同名目录下
File files =
new
File(path +
"\\dishpic"
);
// 判断文件夹是否存在,如果不存在则创建文件夹
if
(!files.exists()) {
files.mkdir();
}
if
(file.getContentType().split(
"/"
)[
0
].equals(
"image"
)) {
if
(path.endsWith(separator))
fileName = path +
"dishpic"
+ separator + uuid +
".png"
;
else
fileName = path + separator +
"dishpic"
+ separator + uuid +
".png"
;
fos =
new
FileOutputStream(fileName);
ImageUtil.convertFormat(fis, fos, fileType,
"png"
,
0
,
0
);
fos.flush();
fos.close();
}
}
catch
(Exception ex) {
System.out.println(
"文件取出失败,错误信息: "
+ ex.getMessage());
if
(fos !=
null
)
fos.close();
throw
ex;
}
return
"dishpic"
+ separator + uuid +
".png"
;
}
/**
* <b>1.对图片时行格式转换</b><br>
* <b>2.对图片进行适度的大小裁剪</b>
*
*/
public
class
ImageUtil {
/**
*
* @param infile 输入文件
* @param outfile 输出文件
* @param srcFormat 源格式
* @param destFormat 输出格式
* @return
* @throws Exception
*/
public
static
boolean
convertFormat(InputStream infile,
OutputStream outfile, String srcFormat, String destFormat,
int
width ,
int
height)
throws
Exception {
boolean
flag =
false
;
BufferedImage src = ImageIO.read(infile);
if
(height >
0
&& width >
0
) {
// compress the origin image if width and height are non-zero
height = src.getHeight() > height ? height: src.getHeight();
width = src.getWidth() > width ? width : src.getWidth();
Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
//这个是用来进行图片大小调整的
BufferedImage tag =
new
BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
//可在下面对图片进行绘制和更改
g.drawImage(image,
0
,
0
,
null
);
// 绘制缩小后的图
g.dispose();
tag.flush();
flag = ImageIO.write(tag, destFormat, outfile);
// 输出到经过缩放的文件流
}
else
{
flag = ImageIO.write(src, destFormat, outfile);
//输出原分辨率的图片
}
Logger.getLogger(ImageUtil.
class
).info(
"图片转换成功: 从["
+ srcFormat +
"]到["
+ destFormat +
"]"
);
return
flag;
}
}
|