jsp頁面:
<div id="UploadPanel" class="easyui-dialog"
data-options="closed:true" style="width: 320px;" title="圖片上傳 ">
<form id="upLoadForm" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td><input type="file" name="image" class="file"
id="fileField" size="28" /></td>
</tr>
<tr>
<td>添加的圖片大小不能超過2MB</td>
<td><a class="easyui-linkbutton"
data-options="plain:true" href="javascript:void(0)" οnclick="FunUpLoad()">上傳</a></td>
</tr>
<tr>
</table>
</form>
注意:紅色字體一定要有,就像jsp動態跳轉上傳一樣
js代碼:
function FunUpLoad(){
if($("#fileField").val() == ""){
alert("請選擇上傳文件!");
return;
}
$("#upLoadForm").form('submit',
{
url : 'mesher_upLoad.action',
onsubmit : function() {
return $(this).form("validate");
},
success : function(data) {
var obj = eval("("+data+")");
$("#UploadPanel").dialog("close");//關閉添加文件頁面
$("img").attr("src",obj.merIntro.gridContent)//將src地址修改爲保存在數據庫中的地址,用來回顯圖片
}
});
}
basicAction中的代碼
/**
* 以源文件類型來保存上傳的文件,使用UUID作爲文件名,並返回爲文件存儲的全路徑
* @param upload
* @param uploadFileName
* @param key
* @return savePath + UUIDfileName
* @throws IOException
*/
public String saveFile(String key, File upload, String uploadFileName)
throws IOException {
long now = new Date().getTime();
if (upload != null) {
// 讀取配置文件中的路徑
Properties pro = new Properties();
InputStream path = Thread.currentThread().getContextClassLoader().//
getResourceAsStream("path.properties");// 獲取路徑並轉換成流
pro.load(path);
String savePath = pro.getProperty(key);// key表示配置文件中的key
//其實如果項目不需要的話沒必要用配置文件區路徑,可以自己直接獲取
// 1.設置保存上傳的文件全部路徑
String uploadPath = ServletActionContext.getServletContext()
.getRealPath("/") + savePath;
// 判斷上傳文件名是否有擴展名
int index = uploadFileName.lastIndexOf('.');
if (index != -1) {
imageFileName = now + uploadFileName.substring(index);
} else {
imageFileName = Long.toString(now);
}
// 使用UUID作爲文件名,以解決重名的問題
String UUIDfileName = Long.toString(now)
+ UUID.randomUUID().toString() + imageFileName;
// 如果文件夾不存在,就創建
File dir = new File(uploadPath);
if (!dir.exists()) {
dir.mkdirs();
}
File destFile = new File(uploadPath, UUIDfileName);
upload.renameTo(destFile);// 移動到目的地
return savePath + UUIDfileName;
}
return null;
}
path.properties文件
#圖片保存路徑
basePath = pages/upload_image/
action代碼(調用basicAction)
public String upLoad() {
try {
//註釋掉的部分代碼也是可以用的,但是沒有改寫文件名,並且不能在別的action中複用
//讀取配置文件
// Properties pro = new Properties();
// InputStream path = Thread.currentThread().getContextClassLoader().//
// getResourceAsStream("path.properties");//獲取路徑並轉換成流
// pro.load(path);
// String savePath = pro.getProperty("basePath");
// String uploadPath =ServletActionContext.getServletContext().getRealPath("/") + savePath;
//
// if (image != null) {
// File savefile = new File(new File(uploadPath), imageFileName);
// if (!savefile.getParentFile().exists())
// savefile.getParentFile().mkdirs();
// FileUtils.copyFile(image, savefile);
// }
String key = "basePath";
String path = saveFile(key,image, imageFileName); //調用basicAction方法
mesherIntro.setGridContent(path);
save();//調用save方法保存文件路徑
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
struts.xml
<action name="mesher_*" class="mesherIntroAction" method="{1}">
<result type="json">
<param name="root">jsonObj</param>
</result>
<interceptor-ref name="fileUpload">
<!-- 文件過濾 -->
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<!-- 文件大小, 以字節爲單位 -->
<param name="maximumSize">1025956</param>
</interceptor-ref>
<!-- 默認攔截器必須放在fileUpload之後,否則無效 -->
<interceptor-ref name="defaultStack" />
</action>