1.组件上传的插件 ajaxfileupload.js,上面是官方的例子
第一个链接是官方的的javascript
http://www.phpletter.com/our-projects/ajaxfileupload/
第二个链接是网上找到的demo,我就不贴上我的的demo啦,差不多
http://www.phpletter.com/our-projects/ajaxfileupload/
2.将上传的文件通过SmbFile传送到共享文件夹,我说一下思路
第一步 现将上传的文件保存到数据库,数据库的字段有三个 (orderId,fileName,fileId)
第二步 通过SmbFile将文件传送到共享文件夹
3.需要文件上传的地方用iframe
4.列出配置文件,Action核心代码,SmbService核心代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="message"></constant> <package name="struts2" namespace="/pages/common" extends="json-default"> <action name="upfileAction" class="com.bq.web.action.UpfileAction"> <param name="fileSavePath">/upload</param> <result type="json"> <param name="contentType">text/html</param> </result> <result name="fileList">/pages/common/commonUpfile.jsp</result> <result name="fileListOEM">/pages/common/commonUpfileOEM.jsp</result> <result name="error">/pages/error.jsp</result> <result name="download" type="stream">/pages/common/commonUpfile.jsp</result> <result name="success" type="stream"> <param name="contentType">application/x-msdownload;charset=ISO8859-1</param> <param name="inputName">inputStream</param> <param name="contentDisposition"> attachment;filename="${downloadChineseFileName}" </param> <param name="bufferSize">4096</param> </result> <result name="transfers" type="stream"> <param name="contentType">application/x-msdownload;charset=ISO8859-1</param> <param name="inputName">transferInputStream</param> <param name="contentDisposition"> attachment;filename="data.csv" </param> <param name="bufferSize">4096</param> </result> <result name="downloadvba" type="stream"> <param name="contentType">application/x-msdownload;charset=ISO8859-1</param> <param name="inputName">outputStream</param> <param name="contentDisposition"> attachment;filename="${filename}" </param> <param name="bufferSize">4096</param> </result> </action> </package> </struts>
public String getDownloadChineseFileName(){ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); String downloadChineseFileName = filename; try { OrderFileRecord r=new OrderFileRecord(); r.setOrderId(orderId); r.setFileId(fileId); List<OrderFileRecord> list=orderFileRecordService.queryOrderFileRecord(r); if(list!=null&&list.size()>=0){ downloadChineseFileName=list.get(0).getFileName(); } // response.setHeader("charset","ISO8859-1"); downloadChineseFileName= new String(downloadChineseFileName.getBytes("GB2312"), "ISO8859-1"); downloadChineseFileName=downloadChineseFileName; // if(downloadChineseFileName.length()>17){ // filename=downloadChineseFileName.substring(downloadChineseFileName.lastIndexOf("."),downloadChineseFileName.length()); // downloadChineseFileName=downloadChineseFileName.substring(0,13)+"..."+filename; // } //downloadChineseFileName=new String(downloadChineseFileName.getBytes(), "UTF-8"); // downloadChineseFileName=java.net.URLEncoder.encode(downloadChineseFileName, "UTF-8"); // response.setHeader( "Content-Disposition", "attachment;filename=" + downloadChineseFileName); }catch(Exception e){ e.printStackTrace(); } return downloadChineseFileName; } public InputStream getInputStream() throws Exception{ return SmbService.getFile(getFileSavePath()+"/"+orderId+"/"+ fileId); // String filePath=getFileSavePath().replaceAll("\\\\", "/")+ "/" +orderId+"/"+ fileId; // FileInputStream fos = new FileInputStream(filePath); // return fos; // return ServletActionContext.getServletContext().getResourceAsStream("/" + fileId); } public InputStream getTransferInputStream() throws Exception{ if(filename==null){ filename=""; } filename=filename.replaceAll("\"\r\n\"", "\"\r\n=\""); InputStream inputStream = new ByteArrayInputStream(filename.getBytes()); // new OutputStreamWriter(null,"); return inputStream; } public InputStream getOutputStream() throws Exception{ InputStream inputStream = null; try{ inputStream = SmbService.getVBAFile( filename); // File f = new File(SmbService.smbVBAFilePathConfig + filename); // inputStream = new FileInputStream(f ) ; }catch(Exception e){ logger.error(e.getMessage()); logger.error(e.getStackTrace()); } return inputStream; } public String downloadvba(){ // File f = new File(SmbService.smbVBAFilePathConfig + filename); SmbFile f = null; try { f = new SmbFile(SmbService.smbVBAFilePathConfig+filename); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(f==null ){ this.addActionError("File does not exit!"); return "error"; } return "downloadvba"; } public String transfers(){ HttpServletRequest request = ServletActionContext.getRequest (); HttpServletResponse response= ServletActionContext.getResponse(); filename=filename.replaceAll("\"\r\n\"", "\"\r\n=\""); try { //如果無 out.clear(); 因重複會java.lang.IllegalStateException: response.setContentType( "application/octet-stream"); response.setHeader("Content-disposition","attachment; filename=data.csv"); //response.setHeader will popup a screen ask where the new file save as, 用response.setHeader會出現popup問CSV會save去那 OutputStreamWriter ow = new OutputStreamWriter(response.getOutputStream(),"GB2312"); //用outputStreamWriter set CSV TO UTF-16LE 這樣excel 開時不會有乱码問題 // ow.write(0xFEFF);//一定要加 ow.write(filename); ow.flush(); ow.close(); response.getWriter().flush(); response.getWriter().close(); }catch(Exception e){ e.printStackTrace(); } return null; // return "transfers"; } public String execute() { return "success"; } public void setFileSavePath(String fileSavePath) { this.fileSavePath = fileSavePath; } public void upload() throws Exception { if (null == getOrderId() || "".equals(getOrderId())) { return; } if (null == filename || "".equals(filename)) { return; } if (null == xls) { return; } OrderFileRecord r = new OrderFileRecord(); r.setFileName(filename); r.setOrderId(orderId); List l = orderFileRecordService.queryOrderFileRecord(r); if (null != l && l.size() > 0) { OrderFileRecord ofr = (OrderFileRecord) l.get(0); deleteRecord(ofr.getFileId(), ofr.getOrderId()); orderFileRecordService.deleteOrderFileRecord(ofr); } try { uploadService.uploadFile(this.getXls(), orderId, getFileSavePath(),filename); // SmbService.putFile(orderId+"/"+ fileId, this.getXls()); HttpServletResponse response = Struts2Utils.getResponse(); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); response.setHeader("Charset", "utf-8"); response.setHeader("Cache-Control", "no-cache"); } catch (Exception e) { e.printStackTrace(); logger.error(e); return; } return; } String getJsonFileNameTableRow(){ if(getOrderId()==null||"".equals(getOrderId())){ return ""; } OrderFileRecord r=new OrderFileRecord(); r.setOrderId(getOrderId()); List l=orderFileRecordList=orderFileRecordService.queryOrderFileRecord(r); if(null==l||l.size()>0){ OrderFileRecord vd=(OrderFileRecord)l.get(0); } return ""; } public String deleteRecord(String fileId,String orderId){ if(fileId==null||"".equals(fileId)){ return queryOrderFile(); }else if(getOrderId()==null||"".equals(getOrderId())){ return queryOrderFile(); } fileId=fileId.trim(); orderId=orderId.trim(); String filePath=getFileSavePath()+"/"+orderId+"/"+ fileId; // String filePath=getFileSavePath().replaceAll("\\\\", "/")+ "/" +orderId+"/"+ fileId; // deleteFile(filePath); SmbService.deleteFile(filePath); OrderFileRecord r=new OrderFileRecord(); r.setOrderId(getOrderId()); r.setFileId(fileId); orderFileRecordService.deleteOrderFileRecord(r); return queryOrderFile(); } public String deleteRecordOEM(String fileId,String orderId){ if(fileId==null||"".equals(fileId)){ return queryOrderFile(); }else if(getOrderId()==null||"".equals(getOrderId())){ return queryOrderFile(); } fileId=fileId.trim(); orderId=orderId.trim(); String filePath=getFileSavePath()+"/"+orderId+"/"+ fileId; // String filePath=getFileSavePath().replaceAll("\\\\", "/")+ "/" +orderId+"/"+ fileId; // deleteFile(filePath); SmbService.deleteFile(filePath); OrderFileRecord r=new OrderFileRecord(); r.setOrderId(getOrderId()); r.setFileId(fileId); orderFileRecordService.deleteOrderFileRecord(r); return queryOrderFileOEM(); } public String deleteRecord(){ if (null != orderId) { orderId=orderId.trim(); } if (null == fileId) { fileId=fileId.trim(); } return deleteRecord(fileId,orderId); } public String deleteRecordOEM(){ if (null != orderId) { orderId=orderId.trim(); } if (null == fileId) { fileId=fileId.trim(); } return deleteRecordOEM(fileId,orderId); } @Deprecated /** * 删除单个文件 * @param fileName 被删除文件的文件名 * @return 单个文件删除成功返回true,否则返回false */ public static boolean deleteFile(String fileName){ File file = new File(fileName); if(file.isFile() && file.exists()){ file.delete(); logger.debug("删除单个文件"+fileName+"成功!"); return true; }else{ logger.debug("删除单个文件"+fileName+"失败!"); return false; } } public String queryOrderFile(){ if(getOrderId()==null){ return fileList; } OrderFileRecord r=new OrderFileRecord(); r.setOrderId(getOrderId().trim()); orderFileRecordList=orderFileRecordService.queryOrderFileRecord(r); return fileList; } private boolean flagOEM=false; public String queryOrderFileOEM(){ if(getOrderId()==null){ return "fileListOEM"; } OrderFileRecord r=new OrderFileRecord(); r.setOrderId(getOrderId().trim()); orderFileRecordList=orderFileRecordService.queryOrderFileRecord(r); if(orderFileRecordList!=null&&orderFileRecordList.size()==1) { flagOEM=true; } return "fileListOEM"; }
package com.bq.utils; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import jcifs.smb.SmbFileOutputStream; import com.bq.exception.CommDevException; public class SmbService { public static String serverFilePathConfig; //"smb://域名;用户名:密码@ip/BPM/"; public static String smbVBAFilePathConfig; public static String smb2DcodePathConfig; public static void putFile(String serverFilePath, File localFile) { SmbFile f; try { f = new SmbFile(serverFilePathConfig+serverFilePath); String parentPath=f.getParent(); if(!f.exists()) { SmbFile parentSmb=new SmbFile(parentPath); if(!parentSmb.exists()) parentSmb.mkdirs(); } FileInputStream in = new FileInputStream(localFile); SmbFileOutputStream out = new SmbFileOutputStream(f); long t0 = System.currentTimeMillis(); byte[] b = new byte[8192]; int n, tot = 0; while ((n = in.read(b)) > 0) { out.write(b, 0, n); tot += n; System.out.print('#'); } long t = System.currentTimeMillis() - t0; System.out.println(); System.out.println(tot + " bytes transfered in " + (t / 1000) + " seconds at " + ((tot / 1000) / Math.max(1, (t / 1000))) + "Kbytes/sec"); in.close(); out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new CommDevException("Put file fail"); } } public static InputStream getFile(String serverFilePath) { try { SmbFile smbfile = new SmbFile(serverFilePathConfig+serverFilePath); SmbFileInputStream in = new SmbFileInputStream(smbfile); return in; } catch (Exception ex) { ex.printStackTrace(); throw new CommDevException("Get file fail"); } } public static void deleteFile(String filePath) { try { SmbFile smbfile = new SmbFile(serverFilePathConfig+filePath); smbfile.delete(); } catch (Exception ex) { ex.printStackTrace(); throw new CommDevException("Get file fail"); } } public String getServerFilePathConfig() { return serverFilePathConfig; } public static InputStream getVBAFile(String serverFilePath) { try { SmbFile smbfile = new SmbFile(smbVBAFilePathConfig+serverFilePath); SmbFileInputStream in = new SmbFileInputStream(smbfile); return in; } catch (Exception ex) { ex.printStackTrace(); throw new CommDevException("Get file fail"); } } public void setServerFilePathConfig(String serverFilePathConfig) { SmbService.serverFilePathConfig = serverFilePathConfig; } public String getSmbVBAFilePathConfig() { return smbVBAFilePathConfig; } public void setSmbVBAFilePathConfig(String smbVBAFilePathConfig) { SmbService.smbVBAFilePathConfig = smbVBAFilePathConfig; } public String getSmb2DcodePathConfig() { return smb2DcodePathConfig; } public static void setSmb2DcodePathConfig(String smb2DcodePathConfig) { SmbService.smb2DcodePathConfig = smb2DcodePathConfig; } }