Domino servlet下载附件 红太狼 2022-05-16 06:08 117阅读 0赞 Domino通过url打开附件内容方法 1)[http://xxx.yyy/abc.nsf/0/1A97B5BBA75AF8FC48257CAF00248EE4/$FILE/ls调用java.doc][http_xxx.yyy_abc.nsf_0_1A97B5BBA75AF8FC48257CAF00248EE4_FILE_ls_java.doc] 方法有个情况,如果是pdf或插件直接浏览器打开。 2)通过servlet打开,避免了方法1的打开问题。 js打开方法 window.open('/servlet/DownUrl?filePath='+encodeURI(filename)+'&dbpath='+dbpath+'&unid='+unid+'&num=0&id='+Math.random()); import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Enumeration; import java.util.Vector; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lotus.domino.Database; import lotus.domino.Document; import lotus.domino.EmbeddedObject; import lotus.domino.NotesException; import lotus.domino.NotesFactory; import lotus.domino.NotesThread; import lotus.domino.RichTextItem; import lotus.domino.Session; public class DownUrl extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } //num 1为servlet动态添加,0为原文档 //window.open('/servlet/DownUrl?filePath='+encodeURI(filename)+'&dbpath='+dbpath+'&unid='+unid+'&num='+num+'&id='+Math.random()); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String num=request.getParameter("num"); //1为servlet动态添加,0为原文档 String filePath = request.getParameter("filePath"); //文件名称 String dbpath= request.getParameter("dbpath"); //数据库路径 String unid= request.getParameter("unid"); //文档unid if (filePath != null && num!=null) { //System.out.println("URLDecode filePath="+URLDecoder.decode(request.getParameter("filePath"), "UTF-8")); filePath = new String(request.getParameter("filePath").getBytes("ISO-8859-1"), "utf-8"); System.out.println("filePath="+filePath); dbpath= new String(request.getParameter("dbpath").getBytes("ISO-8859-1"), "utf-8"); System.out.println("dbpath-->"+dbpath); unid= new String(request.getParameter("unid").getBytes("ISO-8859-1"), "utf-8"); System.out.println("unid-->"+unid); num= new String(request.getParameter("num").getBytes("ISO-8859-1"), "utf-8"); }else{ response.setContentType("text/html;charset=GBK"); response.getWriter().print("<script>alert('请求参数错误!');javascript:history.go(-1);</script>"); return; } String mainPath = gethj()+"/domino/html/attached/"; System.out.println("mainPath="+mainPath); //downLoadFromUrl(filePath,getFileName(filePath),mainPath); String path=mainPath+filePath; try { //如果为原文档提供附件 if(num.equals("0")){ SaveFile( dbpath,unid,path); } } catch (NotesException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String path=filePath; //path="D:/jscss.rar"; File file =new File(path); //File file3 = new File("D:/jscss.rar"); System.out.println(file.getAbsolutePath()); if(path.equals("")){ System.out.println("文件不存在!"); response.setContentType("text/html;charset=GBK"); response.getWriter().print("<script>alert('文件不存在!');</script>"); //response.getWriter().print("<script>window.opener.close();</script>"); response.getWriter().print("<script>javascript:history.go(-1);</script>"); return; }else{ downloadFile(path, getFileName(path), response,num); } } /* * num //1为servlet动态添加,0为原文档 */ private static void downloadFile(String filePath, String fileName, HttpServletResponse response,String num) throws IOException { //设置响应头和保存文件名 response.setContentType("APPLICATION/OCTET-STREAM;charset=gbk"); // response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); BufferedInputStream bis=null; BufferedOutputStream bos=null; try{ ServletOutputStream sos=response.getOutputStream(); bis=new BufferedInputStream(new FileInputStream(filePath)); //bis=new BufferedInputStream(inputStream); bos=new BufferedOutputStream(sos); byte[] buffer=new byte[4096]; int byteread; while(-1!=(byteread=bis.read(buffer,0,buffer.length))){ bos.write(buffer, 0, byteread); } }catch(IOException e){ e.printStackTrace(); }finally{ if(bis!=null){ bis.close(); } if(bos!=null){ bos.close(); } System.out.println("关闭流!"); } System.out.println("文件下载完毕."); //如果为原文档提供附件 if(num.equals("0")){ deleteFile(filePath); } } //截取文件名称 private String getFileName(String filePath){ int index1=filePath.lastIndexOf("/"); int index2=filePath.lastIndexOf("\\"); int index=(index1>index2)?index1:index2; return filePath.substring(index+1); } /** * 从输入流中获取字节数组 * @param inputStream * @return * @throws IOException */ public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); } /** * 从网络Url中下载文件 * @param urlStr * @param fileName * @param savePath * @throws IOException */ public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{ URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //设置超时间为3秒 conn.setConnectTimeout(3*1000); //防止屏蔽程序抓取而返回403错误 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //得到输入流 InputStream inputStream = conn.getInputStream(); //获取自己数组 byte[] getData = readInputStream(inputStream); //文件保存位置 File saveDir = new File(savePath); if(!saveDir.exists()){ saveDir.mkdir(); } File file = new File(saveDir+File.separator+fileName); FileOutputStream fos = new FileOutputStream(file); fos.write(getData); if(fos!=null){ fos.close(); } if(inputStream!=null){ inputStream.close(); } System.out.println("info:"+url+" download success"); } public String gethj(){ try { //创建NotesThread对象 NotesThread.sinitThread(); //创建新会话对象 Session session = NotesFactory.createSession(); String temp= session.getEnvironmentString("Directory", true); session.recycle(); return temp; } catch (NotesException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("操作异常"); System.out.println(e.toString()); return ""; } finally { NotesThread.stermThread(); } } /** * * @param dbpath 数据库路径 * @param unid 文档unid * @param filepath 文件完整路径 * @throws NotesException */ public void SaveFile(String dbpath,String unid,String filepath) throws NotesException{ NotesThread.sinitThread(); Session session = NotesFactory.createSession(); System.out.println("1111"); Database db = session.getDatabase("", dbpath); System.out.println("unid-->"+unid); System.out.println("database-->"+db.getFileName()); Document doc = db.getDocumentByUNID(unid); System.out.println("doc-->"+doc.getUniversalID()); RichTextItem body = (RichTextItem) doc.getFirstItem("Body"); if (body != null) { //如果新旧附件同名 System.out.println("进入附件名称与新附件名称相同-->"); Vector v = body.getEmbeddedObjects(); Enumeration e = v.elements(); while (e.hasMoreElements()) { EmbeddedObject eo = (EmbeddedObject) e.nextElement(); if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) { //判断 有同样的附件 System.out.println("eo.getName()附件名称-->"+eo.getName()); //eo.remove(); deleteFile(filepath); eo.extractFile(filepath); break; } } /*while结束*/ }else{ System.out.println("文档没有对应的Body富域,建议附件文档时,附件在富域Body里面!"); } //回收对象 db.recycle(); session.recycle(); NotesThread.stermThread(); } /** * 删除单个文件 * @param sPath 被删除文件的文件名 * @return 单个文件删除成功返回true,否则返回false */ public static boolean deleteFile(String sPath) { boolean flag = false; File file = new File(sPath); // 路径为文件且不为空则进行删除 if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag; } } 参考: [https://blog.csdn.net/woshiwxw765/article/details/6653476][https_blog.csdn.net_woshiwxw765_article_details_6653476] [http_xxx.yyy_abc.nsf_0_1A97B5BBA75AF8FC48257CAF00248EE4_FILE_ls_java.doc]: http://ibmx366:8000/kpoa/app/hoa_flowtest.nsf/0/1A97B5BBA75AF8FC48257CAF00248EE4/$FILE/ls%E8%B0%83%E7%94%A8java.doc [https_blog.csdn.net_woshiwxw765_article_details_6653476]: https://blog.csdn.net/woshiwxw765/article/details/6653476
还没有评论,来说两句吧...