您的位置:首页 > 其它

uploadify文件上传的使用

2015-04-15 17:51 204 查看
1.jsp代码

appname=$("#appname").val();
$("#uploadify").uploadify({
'swf' : '<%=basePath%>/Backstage/Style/jQuery-uploadify/uploadify.swf',
// 'script' : 'ProductAction!fileUpload.do',//后台处理的请求
'uploader': 'AppVersionAction!fileUpload.do?appname='+appname ,
'cancelImg' : '<%=basePath%>/Backstage/include/easyvalidator/images/vtip_arrow.png',
'folder' : '/uplaod/temp',//您想将文件保存到的路径
'queueID' : 'fileQueue',//与下面的id对应
'fileObjName':'uploadify',
'queueSizeLimit' : 5,
'fileDesc' : '文件格式',
'fileTypeExts' : '*.apk;*.ipa;*.png', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : '选择文件',
'onQueueComplete' : function(file,data,response) {
picFlag = true;
}
});


<input type="file" name="uploadify" id="uploadify" />
<input id="appname" type="text" value="<s:property value="version.appName"/>" name="version.appName" size="30"   maxlength="32"  data-options="required:true,missingMessage:'请输入版本号'" class="easyui-validatebox" /><font color="red"> *</font>


2.action和service

public void addVersion() {
String appname= getRequest().getParameter("appname");
UploadImg upload = new UploadImg();
try {
/** 商品基本属性 **/
version.setId(Constants.getUUID());
version.setCreateTime(Constants.getSimpleDateFormat3());

/** 将缓存中的图片放入版本目录中 **/
List<String> files = (List<String>) getSession().getAttribute("files");
String filesext=(String) getSession().getAttribute("fileExt");
String updateUrl=(String) getSession().getAttribute("fileName1");
if (files != null) {
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyyMMdd");
String folderName = Constants.versionFileSavePath + "/" + yearFormat.format(new Date()) ;
String newFileName=appname+"."+filesext;
for (int i = 0; i < files.size(); i++) {
upload.moveAndSaveFile(getRequest(), "/Upload/temp" +files.get(i), "/Upload/"+folderName,newFileName);
}
version.setUpdateUrl(updateUrl);
//                commodityInfo.setImageDir("/Upload/" + folderName);
}
getSession().removeAttribute("files");
getSession().removeAttribute("fileName1");
this.wfAppVersionService.addObject(version);
String json = JSON.toJSONString(version);
json = json.replace("}", ",\"tips\":\"新增版本信息成功\"}");
getResponse().setContentType("text/html;charset=utf-8");
getResponse().getWriter().println(json);
} catch (Exception ex) {
ex.printStackTrace();
try {
getResponse().setStatus(555);
getResponse().getWriter().println("新增失败");
Logger.getLogger(AppVersionAction.class).info("版本控制执行新增操作异常");
} catch (IOException e) {
e.printStackTrace();
}
}
}


public void copyAndUpateFile(HttpServletRequest request,String oldPath, String newPath, String newName) {
try {
int bytesum = 0;
int byteread = 0;
System.out.println("@@newPath:  "+newPath);
File oldfile = new File(request.getRealPath("/")+oldPath);
if (oldfile.exists()) { // 文件存在时
//查看传入的目录是否存在 如果不存在就创建
if (newPath.split("/").length > 0) {
String[] paths = newPath.split("/");
String path = request.getRealPath("/") ;
for (int i = 0; i < paths.length; i++) {
path += paths[i] + "/";
File saveDirFile = new File(path);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
}
}
File file=new File(request.getRealPath("/")+newPath+"/"+"cbt_"+newName);
file.createNewFile();
InputStream inStream = new FileInputStream(request.getRealPath("/")+oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(request.getRealPath("/")+newPath+"/"+"cbt_"+newName);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();

}
}

/**
* 将临时文件tmp移动并改变格式
* @Title:  UploadImg
* @param:  @param request
* @param:  @param oldPath
* @param:  @param newPath
* @param:  @param newName
* @return:
* @throws:
* @Description: TODO
*/
public void moveAndSaveFile(HttpServletRequest request,String oldPath, String newPath,String newName) {
copyAndUpateFile(request,oldPath, newPath,newName);
String fileExt = oldPath.substring(oldPath.lastIndexOf(".") + 1).toLowerCase();
System.out.println("oldPath:      "+oldPath+"###NewPath:  "+newPath+" fileExt:   "+fileExt);
delFile(request,oldPath);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: