



有圖有真相
這是少爵利用空余時間寫的 easyui + swfupload 多文件上傳的頁面效果.
是個半成品 .任然有些問題 .有些文件上傳成功卻 0%. 有些文件上傳錯誤 卻 100%
當文件無后綴時. 上傳卻是不行.停止功能暫時不能用.取消所有可以使用.
不允許選擇存在於上傳隊列中的文件繼續上傳. 會彈出提示 .文件超大小.會提示 .
后台函數如下:
1 public void UPLOADFILED() { 2 Date dt = new Date(System.currentTimeMillis()); 3 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); 4 String fileName = sdf.format(dt); 5 int index = getUploadFileName().lastIndexOf("."); 6 //得到文件擴展名 7 String extendName = getUploadFileName().substring(index); 8 String path = getRootPath()+"up\\"; 9 //String sessionId = getRequest().getSession().getId(); 10 String filename = fileName + extendName; 11 Json j = uploadFile(filename, path, 200 * 1024 *1024, true); 12 try { 13 String json = JSON.toJSONStringWithDateFormat(j,"yyyy-MM-dd"); 14 ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); 15 ServletActionContext.getResponse().getWriter().write(json); 16 ServletActionContext.getResponse().getWriter().flush(); 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 22 //writeJson(json); 23 } 24 25 26 /*public String uploadFile(String path){ 27 return uploadFile(getUploadFileName(), path, 500 * 1024, false); 28 }*/ 29 30 /** 31 * 上傳文件 32 * @param filename 文件名 33 * @param path 文件保存路徑 34 * @param maxSize 上傳文件的最大大小 35 * @param overwrite 是否覆蓋已存在的文件 36 * @return 37 */ 38 public Json uploadFile(String filename, String path, long maxSize, boolean overwrite){ 39 Json j = new Json(); 40 JSONObject jsonObject = new JSONObject(); 41 JSONArray jsonArray = new JSONArray(); 42 String fileName = filename; 43 String msg = "文件上傳成功!"; 44 if (! overwrite) { 45 //檢查並得到新的保存文件名,防止重名后覆蓋已存在的文件 46 fileName = FileUtils.checkFileName(filename, path); 47 if (FileUtils.isFileExist(fileName, path)) { 48 /*FileDownloadUtils.downloadJSON("{success:false, msg:'上傳文件名已存在,請改名后重新上傳!'}", 49 getResponse());*/ 50 msg = "上傳文件名已存在,請改名后重新上傳!"; 51 return null; 52 } 53 } 54 if (upload == null) { 55 /*FileDownloadUtils.downloadJSON("{success:false, msg:'文件名及路徑名有問題,請修改后重新上傳!'}", 56 getResponse());*/ 57 msg = "文件名及路徑名有問題,請修改后重新上傳!"; 58 return null; 59 } 60 try { 61 if (upload.length()> maxSize) { 62 /*FileDownloadUtils.downloadJSON("{success:false, msg:'上傳文件不能大於" + maxSize +",請修改后重新上傳!'}", 63 getResponse());*/ 64 msg = "上傳文件不能大於" + maxSize +",請修改后重新上傳!"; 65 return null; 66 } 67 FileUtils.uploadForName(fileName, path, upload); 68 } catch (IOException e) { 69 e.printStackTrace(); 70 /*FileDownloadUtils.downloadJSON("{success:false, msg:'文件上傳失敗!'}", 71 getResponse());*/ 72 msg = "文件上傳失敗!"; 73 return null; 74 } 75 j.setSuccess(true); 76 j.setMsg(msg); 77 jsonObject.put("name", fileName); 78 jsonObject.put("type", FileUtils.converContentType(getUploadContentType())); 79 jsonObject.put("size", upload.length()); 80 jsonArray.add(jsonObject); 81 j.setObj(jsonArray); 82 /*FileDownloadUtils.downloadJSON( 83 "{success:true, msg:'文件上傳成功!', " + 84 "file: {name:'" + fileName + 85 "', type:'" + FileUtils.converContentType(getUploadContentType()) + 86 "', size:" + upload.length() + "}}", 87 getResponse());*/ 88 89 //errMessage="文件上傳成功!"; 90 //success = true; 91 return j; 92 }
