为了 app 整合, 原先用AGENT 去 接收档案,却是乱码,无法正常解析
后来只能改用 JAVA 进行接收档案
建立 一份 xpage 及一个 java
测试:
Postman
POST
form-data
key: files (file) , title (text)
执行结果: 产生一份 notes 文件
form : test
files: richtext
filesname :text
title: text
//*The XPage is simple *//
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
xp:this.beforePageLoad
</xp:this.beforePageLoad>
</xp:view>
//*JAVA *//
//*使用JAVA 汇入 扩充的 Class 至主机上 \Lotus\Domino\osgi\shared\eclipse\plugins *//
package eu.linqed;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lotus.domino.*;
import com.ibm.xsp.http.UploadedFile;
import com.ibm.xsp.webapp.XspHttpServletResponse;
public class UploadHandler {
// https://stackoverflow.com/questions/19723731/how-to-upload-a-file-from-mobile-phone-camera-to-xpages-with-phonegap-filetransf参考网址
//http://XXXXX/Database/alison/XpageFile-upload.nsf/UploadHandler.xsp
private static String FieldName1 = "title" ; // 第一个栏位
private static String FILE_PARAM = "files"; // name of the multipart
// POST param that holds
// the uploaded file
private static String RT_ITEM_NAME_FILES = "files"; // name of the RT item
// that will hold the
// uploaded file
public UploadHandler() {
}
@SuppressWarnings("unchecked")
public static void process() {
System.out.println("eu.linqed.UploadHandler**********************");XspHttpServletResponse response = null;PrintWriter pw = null;UploadedFile uploadedFile = null;File correctedFile = null;RichTextItem rtFiles = null;Document doc = null;String fileName = "";
// String title = "" ;
FacesContext facesContext = FacesContext.getCurrentInstance();System.out.println("FacesContext");try { ExternalContext extCon = facesContext.getExternalContext(); System.out.println("ExternalContext"); response = (XspHttpServletResponse) extCon.getResponse(); System.out.println("response"); pw = response.getWriter(); //only HTTP POST is allowed HttpServletRequest request = (HttpServletRequest) extCon.getRequest(); System.out.println("HttpServletRequest"); if (!request.getMethod().equalsIgnoreCase("post")) { throw (new Exception("only POST is allowed")); } Database dbCurrent = (Database) resolveVariable("database"); response.setContentType("text/plain"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", -1); //check if we have a file in the POST System.out.println("getParameterMap"); Map map = request.getParameterMap(); System.out.println("Map"); //System.out.println(" find: "+ map.get("title").toString()); if (!map.containsKey(FILE_PARAM)) { System.out.println("FILE_PARAM - no file"); throw (new Exception("no file received")); } //******************************** //get the file from the request System.out.println("uploadedFile"); uploadedFile = (UploadedFile) map.get(FILE_PARAM); System.out.println(FILE_PARAM); if (uploadedFile == null) { throw (new Exception("that's not a file!")); } //store file in a document fileName = uploadedFile.getClientFileName(); //original name of the file File tempFile = uploadedFile.getServerFile(); // the uploaded file with a cryptic name System.out.println(fileName+"-"+tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName ); //we rename the file to its original name, so we can attach it with that name //see http://www.bleedyellow.com/blogs/m.leusink/entry/processing_files_uploaded_to_an_xpage?lang=nl correctedFile = new java.io.File(tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName); boolean renamed = tempFile.renameTo(correctedFile); // 如果不执行 就会抓不到档案 if (renamed) { //create a document in the current db doc = dbCurrent.createDocument(); doc.replaceItemValue("form", "test"); doc.replaceItemValue(FieldName1, map.get(FieldName1).toString()); doc.replaceItemValue("FileName", fileName); //档案名称 //attach file to target document rtFiles = doc.createRichTextItem(RT_ITEM_NAME_FILES); rtFiles.embedObject(lotus.domino.EmbeddedObject.EMBED_ATTACHMENT, "", correctedFile.getAbsolutePath(), null); boolean saved = doc.save(); if (saved) { pw.print("SAVE OK"); } }else{ pw.print("add code to return to the upload method here-->"+ map.get( FieldName1)); } response.commitResponse();} catch (Exception e) {System.out.println(e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); pw.print("add code here to return an error -->" +e ); try { response.commitResponse(); } catch (IOException e1) { e1.printStackTrace(); }} finally { facesContext.responseComplete(); try { if (rtFiles != null) { rtFiles.recycle(); } if (doc != null) { doc.recycle(); } if (correctedFile != null) { // rename temporary file back to its original name so it's // automatically // deleted by the XPages engine correctedFile.renameTo(uploadedFile.getServerFile()); } } catch (Exception ee) { ee.printStackTrace(); }}
}
private static Object resolveVariable(String variableName) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, variableName);
}
}