js
$(document).ready(function() {
$("#apkIcon").uploadify({
'swf' : 'js/upload/uploadify.swf',
'uploader' : 'action_upload.action',
'cancel' : 'js/upload/uploadify-cancel.png',
'buttonClass' : 'uploadify-button',
'fromData' : '{'id':'${"#apkIcon"}.val()'}',
'fileObjName' : 'apkIcon',
'buttonImage' : 'js/upload/apk_ctw.gif',
'progressData' : 'percentage',
'removeCompleted': false,
'method' : 'post',
'fileTypeExts' : '*.png;*.gif;',
'width' : '65',
'height' : '30',
'fileSizeLimit' : '256KB',
'auto' : true,
'multi' : true,
'onUploadStart' : function(file) {
alert(file.name);
$("#icon").attr("value",file.name);
alert($("#icon").val());
},
onUploadSuccess:function(file,data,response){
if(response){
$("#icondiv")("<img src='"+data+"'>");
}else{
alert('上传发生错误');
}
},
'onUploadError':function(file,errorCode,errorMsg,errorString){
alert('只能上传不大于256KB的图片文件');
},
'onQueueComplete': function (event, data) {
// if(data.filesUploaded>=1){
// alert('上传成功');
// }
}
});
});
jsp:
<input type="file" id="apkIcon" name="apkIcon" >
action
public void upload(){
//File imageFile=new File(ServletActionContext.getServletContext().getRealPath("/uploads")+"/"+param);
//upload(up, imageFile);
try {
// 用于设定诸如缓存之类的参数,和性能相关
// 此处用默认设定
DiskFileItemFactory dfif = new DiskFileItemFactory();
// 解析表单中的数据
ServletFileUpload upload = new ServletFileUpload(dfif);
//upload.setSizeMax(10 * 1024 * 1024); // 允许上传的最大值
List list =upload.parseRequest(ServletActionContext.getRequest()); // 开始解析request对象中的表单数据
String id=ServletActionContext.getRequest().getParameter("id");
// list中是FileItem对象
// 一个FileItem用于封装一个上传的文件数据
if (list.size() >= 1) {
FileItem item = (FileItem) list.get(0);
// 获得上文件的路径名
String name = item.getName();
name = name.substring(name.lastIndexOf("\\") + 1);
// 把上传的文件数据写入本地文(服务器端)件文件夹的名字为upload
String path = "uploads";
// Sun的标准,服务器实现的API
ServletContext ctx = ServletActionContext.getServletContext();
path = ctx.getRealPath(path);
File file = new File(path);
if(!file.exists()){
System.out.println("创建文件夹");
file.mkdir();
}
System.out.println(path);
System.out.println(name);
//将文件放到指定的地方
item.write(new File(path, name));
}
} catch (Exception e) {
System.out.print("文件上传失败");
}
String savePath=ServletActionContext.getServletContext().getRealPath("")+"/uploads/";
try{
File folder=new File(savePath);
String filePath="C:\\Documents and Settings\\Administrator\\桌面\\R7TU[}VOOFGQQ~0)MY3IQJI.gif";//ServletActionContext.getRequest().getParameter("apkVO.apk.apkIcon");
if(!folder.exists()){
folder.mkdirs();
}
// File outFile=new File(savePath));
FileOutputStream outStream = new FileOutputStream(folder);