在CentOS linux上的Lift中发送带有附件的电子邮件

在CentOS linux上的Lift中发送带有附件的电子邮件,linux,scala,lift,Linux,Scala,Lift,我正在尝试上载一个文件,并将其作为电子邮件附件发送。我的 代码在windows操作系统上运行,但当我尝试在Linux服务器上运行它时,它 创建文件,但无法将其附加到电子邮件。知道为什么吗 在Linux服务器下发生什么? 下面是我的代码: class ClientUpload extends Logger { object excelFile extends RequestVar[Box[FileParamHolder]](Empty) private val aRandomTargetF

我正在尝试上载一个文件,并将其作为电子邮件附件发送。我的 代码在windows操作系统上运行,但当我尝试在Linux服务器上运行它时,它 创建文件,但无法将其附加到电子邮件。知道为什么吗 在Linux服务器下发生什么? 下面是我的代码:

  class ClientUpload extends Logger {

object excelFile extends RequestVar[Box[FileParamHolder]](Empty)

private val aRandomTargetFile = Random.nextLong().toString + ".xls"

 private def importExcel(name:String,sheetName:String): List[String] = {
println("import excel sheet="+sheetName+"name="+name)
var response : List[String] = Nil
var currRow = 0
if(sheetName == "Form")
           currRow = firstRow
else if(sheetName == "Form2")   
           currRow = firstRow+1
if(currRow != 0){
try{
   var workbook = new HSSFWorkbook(new FileInputStream(name))
    val sheet = workbook.getSheet(sheetName)

    try{

        var endOfSheet_? : Boolean = false
        val style = workbook.createCellStyle();
        style.setFillBackgroundColor(HSSFColor.RED.index)
        style.setFillPattern(1)
        while (!endOfSheet_?) {
           val row = sheet.getRow(currRow)
           println("row="+row.getCell(0))
           if (row != null &&
                row.getCell(0) != null &&
                row.getCell(0).getNumericCellValue() > 0 ) {

              info("Processing row "+ currRow)                

            }
            else
            {
                 endOfSheet_? = true
            }
           currRow = currRow + 1
        }

      }catch {
            case e: Exception => { }
            case _ => { response = "Problem at cell 0 of row " + currRow :: response }
    }


      var fos: FileOutputStream = null;

        fos = new FileOutputStream(new File("/tmp/"+aRandomTargetFile));

    workbook.write(fos);
} catch {
     case e: IOException => { response = "Problem opening or closing the excel sheet : " + e.getMessage() :: response }
     case _ => { response = "Problem opening or closing the excel sheet : " + sheetName :: response}
}
}
   response.reverse
}
def render ={
def process() {

  excelFile.is match {

  case Empty => S.error(S.?("upload.error.missing"))

  case Full(exf) => {
    var  result : List[String] = Nil
      info("Proccessing excel file")
      info("Meme type="+exf.mimeType)
     val basepath = "/tmp/" 
     val aRandomTargetFile = Random.nextInt(Integer.MAX_VALUE) + ".xls"

      val oFile = new File(basepath, aRandomTargetFile) 

      val output = new FileOutputStream(oFile)
      output.write(exf.file)
      output.close()

      result = result ++ importExcel(oFile.getPath(),"Form")


      if(!result.isEmpty)
        S.error(<li>{"Found the following problems:"}</li>++result.map(r => <li>{r}</li>))
      else
        sendEmail() &
        S.notice("Successfully sent your upload.") 

    }
   case _ => S.error(S.?("upload.error"))
  }
}

uploadExcel &
"type=submit" #> SHtml.onSubmitUnit(process)
}


 def uploadExcel: CssBindFunc = {

(S.get_?, excelFile.is) match {
  case (true, _)  => "name=file" #> SHtml.fileUpload(s => excelFile(Full(s)),("class"  -> "btn btn-file"))
  case (_, Empty) => "name=file" #> SHtml.fileUpload(s => excelFile(Full(s)),("class" -> "btn btn-file"))
  case (false, _) => "name=file" #> SHtml.link(S.hostAndPath + "/tmp/" + aRandomTargetFile,() => Unit , <span></span>) &   
  "type=submit" #> SHtml.ajaxButton("Upload another file", ()=>S.redirectTo("clientupload"))  
}
}

def sendEmail(): JsCmd = {


      val body = sroiEmailBody
    val fileLoc="/tmp/"+aRandomTargetFile
    println(fileLoc)
val msg =  
  for ( bytes <- LiftRules.loadResource(fileLoc) )
 yield XHTMLPlusImages(<p>Dear user,<br/><br/>The enclosed file has been sent for review</p>,PlusImageHolder(aRandomTargetFile, "text/csv", bytes) )


 msg match {
case Full(m) =>

      Mailer.sendMail(
            From(emailFrom),
            Subject("Upload"),
            To(emailTo), 
            CC(userEmail),
            PlainMailBodyType(body),
            m)

         case _ =>Alert("There was a problem !") 
  }

JsCmds.RedirectTo("clientupload")        


}

我不确定为什么它在windows中工作,但我认为LiftRules.loadResource使用类加载器来检索与webapp相关的文件,而/tmp/不在其中。你试过直接读取文件吗?也许有这样一种解决方案:实际上,如果我不使用随机文件,而是使用一个特定的名称,那么附件可以工作,但是如果我上传一个新的不同文件,它会在tmp文件夹中创建它,但会在电子邮件中发送一个旧文件。这意味着它可以读取和覆盖文件。我想旧文件还在记忆中!你能把全部代码都显示出来吗?sendEmail在AFAICT进程之外,因此前者不能使用后者中定义的aRandomTargetFile。当然,我已经更新了代码。