How to store uploaded file to local file system using xPages upload control?

时光怂恿深爱的人放手 提交于 2019-12-24 03:15:36

问题


How to store uploaded file to local disk using xPages upload control? I have just a simple form with text field and fileUpload control on my xPages.(there are no binding to document so I'm accessing xpages components to get their values on submit) How can I access such uploaded file from my java code? I can get this upload control from my java code so I have 'XspFileUpload' object. But I cant see any way how to access the raw File object to be able to save it on files system ... Can someone help me with this?


回答1:


To retrieve a file from an upload control you can use this piece of code ( its java so you need to convert it to ssjs..)

// get file from httpservletrequest 

HttpServletRequest hsr = (HttpServletRequest) FacesContext      .getCurrentInstance().getExternalContext().getRequest();
fileUploadID = 'XspFileUpload control'.getClientId(FacesContext.getCurrentInstance());
Map<?, ?> map = hsr.getParameterMap();
UploadedFile f = ((UploadedFile) map.get(fileUploadID));

if (f == null) {
  throw new java.lang.Exception("File could not be found");
}

String fileName = f.getServerFileName()
if (super.isValid() && !this.isHidden()) {
   File serverFile = f.getServerFile();
   if (serverFile != null && serverFile.exists()) {
       String dir = serverFile.getParent();
       File tempFile = new File(dir + File.separator + fileName); // create a handle to the file on server 
   }
}



回答2:


This is the SSJS code written using the answer from jjtbsomhorst, and the code from http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Work_with_a_file_upload_and_download_controls

var con = facesContext.getExternalContext(); 
var request:com.sun.faces.context.MyHttpServletRequestWrapper = con.getRequest(); 
var map:java.util.Map = request.getParameterMap(); 
var fileDataName = "view:_id1:file"; 
var fileData:com.ibm.xsp.http.UploadedFile = map.get( fileDataName ); 

if (fileData == null) {
    getComponent("message").value = "File could not be found on " + fileDataName;
}

var fileName = fileData.getServerFileName();
var serverFile:java.io.File = fileData.getServerFile();
if (serverFile != null && serverFile.exists()) {
    var dir = serverFile.getParent();
    var tempFile:java.io.File = new java.io.File(fileName);
    var correctedFileName = dir + java.io.File.separator + fileData.getClientFileName();
    var correctedFile:java.io.File = new java.io.File( correctedFileName ); 
    var success = tempFile.renameTo(correctedFile);
    getComponent("message").value = "Yay!" + correctedFileName;
    //correctedFile.renameTo(tempFile);
}
else {
    getComponent("message").value = "There's a problem to find the temporal file.";
}

PS. There's a label named "message" in the XPage.



来源:https://stackoverflow.com/questions/9482085/how-to-store-uploaded-file-to-local-file-system-using-xpages-upload-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!