//后台方法
// 打开上传文件窗口
@RequestMapping(value = "/uppic.view", method = RequestMethod.GET)
public ModelAndView showPicUp() {
ModelAndView view = new ModelAndView("admin/rollingpicture/UploadFile");
return view;
}
@RequestMapping(value = "/uppic.do", method = RequestMethod.POST)
public ModelAndView upPic(@RequestParam("picfile") MultipartFile file,
HttpServletRequest request) throws Exception {
ModelAndView view = new ModelAndView("admin/rollingpicture/UploadFile");
ResultDto dto = new ResultDto();
if (!file.isEmpty()) {
String originalFilename = file.getOriginalFilename();
int index = originalFilename.lastIndexOf(".");
String suffix = "";
if (index > -1) {
suffix = originalFilename.substring(index);
}
if (StringUtils.isEmpty(suffix)
|| (!suffix.toLowerCase().endsWith(".jpg")
&& !suffix.toLowerCase().endsWith(".jpge")
&& !suffix.toLowerCase().endsWith(".gif") && !suffix
.toLowerCase().endsWith(".png"))) {
dto.setSuccess(false);
dto.setMessage("图片类型必须是.jpg, .gif, .png");
} else {
String fileRealPathDir = pictureImagePath;
File saveFileDir = new File(fileRealPathDir);
if (!saveFileDir.exists()) {
saveFileDir.mkdirs();
}
String fileName = UUID.randomUUID().toString() + suffix;
File picFile = new File(fileRealPathDir + File.separator + fileName);
file.transferTo(picFile);
Map<String, String> data = new HashMap<String, String>();
data.put("pic", fileName);
dto.setData(data);
}
} else {
dto.setSuccess(false);
dto.setMessage("请选择文件");
}
view.addObject("result", dto);
return view;
}
//访问图片路径
@Value("#{systemConfig['path.product.image']}")
private String productImagePath;
@Value("#{systemConfig['path.upload.file']}")
private String upFilePath;
@RequestMapping("images/product/{imagepath}.{extendsName}")
public void getProductImage(@PathVariable("imagepath") String imagepath, @PathVariable("extendsName") String extendsName, OutputStream os) throws Exception {
File file = new File(productImagePath + imagepath + "." + extendsName);
if(file.exists() && !file.isDirectory()) {
InputStream input = null;
try {
input = new BufferedInputStream(new FileInputStream(file));
byte[] data = new byte[1024];
int len = 0;
while((len = input.read(data))>0 ){
os.write(data, 0, len);
os.flush();
}
} finally {
if(input != null) {
input.close();
}
}
}
}
//页面1
<%@ page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="${ctx}/resources/js/jquery.min.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
if (top == window) {
window.location.href = "${ctx}/${adminPath}/main.html";
}
if ("${result.success}".length > 0) {
var success = eval("${result.success}");
if (success) {
$("#pictureDiv", top.document)
.html(
"<img src='${ctx}/images/picture/${result.data.pic}'>");
$("#picUrl", top.document).val("${result.data.pic}");
} else if ("${result.message}".length > 0) {
top.msgShow("Error", "${result.message}", "error");
}
}
</script>
</head>
<body style="width: 100%; height: 100%">
<form method="post" action="${ctx}/${adminPath}/rollingPicture/uppic.do"
enctype="multipart/form-data">
<input type="file" name="picfile" id="picfile" /><input type="submit"
value="上传文件" />
</form>
</body>
</body>
</html>
//页面2
<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加图片</title>
</head>
<body>
<br/>
<table class="Table_xt" align="center" border="0" cellpadding="0"
cellspacing="0" width="600">
<tr>
<td align="left" bgcolor="#d4e8fa" height="20">
<form id="newPic" method="post" enctype="multipart/form-data">
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td align="right" bgcolor="#FBFDFF" valign="middle" width="108"><span
style="color: #FF0000;">*</span>图片名称</td>
<td align="left" bgcolor="#FBFDFF" height="38" valign="middle">
<input name="picName" id="picName" maxlength="100"
value="" type="text"> <font color="red"><span id="error_picName"
class="error"></span></font>
</td>
</tr>
<td align="right" bgcolor="#FBFDFF" valign="middle">图片</td>
<td align="left" bgcolor="#FBFDFF" height="38" valign="middle">
<iframe src="${ctx}/${adminPath}/rollingPicture/uppic.view"
scrolling="no" frameborder="no"
style="width: 100%; height: 50px; frameborder: no; border: 0; marginwidth: 0; marginheight: 0; allowtransparency: true;"></iframe>
<input type="hidden" name="picUrl" id="picUrl">
<div id="pictureDiv"
style="width: 300px; height: 150px; overflow: auto; border: 1px dashed #990033;">
<center>没有上传产品图片</center>
</div> <font color="red"><span id="error_picturePic" style="color: red;"></span></font>
</td>
</tr>
<tr>
<td align="center" height="50" style="background: #FBFDFF;" valign="middle" colspan="2"><a
href="javascript:void(0)" class="easyui-linkbutton"
οnclick="newPicture('${ctx}','${adminPath}');" icon="icon-save">确认添加</a>
<a href="javascript:void(0)" class="easyui-linkbutton"
icon="icon-cancel" οnclick="closeWindow();">取消</a></td>
</tr>
</table>
</body>
</html>
//js
function newPicture(ctx,adminPath) {
var data = $('#newPic').serializeObject();
var name=data.picName;
if(name.indexOf(" ")>=0){
alert("图片名称不能有空格");
}
$.ajaxPost(ctx+"/"+adminPath+"/rollingPicture/add.ajax", $.toJSON(data), function(result) {
if (handleResult(result)) {
closeWindow();
reloadDataGrid("tt");
}
});
}