2012-03-01 78 views
0

我想在郵件中附加一個csv文件(grails)在郵件中附加CSV

路徑中的文件已經存在。我正在使用以下代碼

sendMail { 
    multipart true 
    from "$senderName <$fromAddress>" 
    to toAddress 
    cc message.cc 
    subject message.subject 
    body content.plaintext 
    html content.html 
    attachBytes './web-app/ReadyOrdersFor-${vendor.name}','text/csv', new File('./web-app/ReadyOrdersFor-${vendor.name}').readBytes() 
} 

錯誤提示是。
java.io.FileNotFoundException:./web-app/ReadyOrdersFor-${vendor.name}.csv(沒有這樣的文件或目錄)

既不這個作品提示了同樣的錯誤

attachBytes'./web-app/ReadyOrdersFor-${vendor.name}.csv','text/csv',new File('./ web-app/ReadyOrdersFor - $ {vendor.name} .csv') .readBytes()

+0

你確定這個文件存在指定路徑嗎? – 2012-03-01 08:55:27

回答

1

問題是,您嘗試使用文件路徑字符串作爲GStringImpl,但字符串是用單引號括起來。 GStringImpl在Groovy中用雙引號原生支持。

你的代碼應該是

attachBytes "./web-app/ReadyOrdersFor-${vendor.name}",'text/csv', new File("./web-app/ReadyOrdersFor-${vendor.name}").readBytes() 

This link應該幫助您瞭解常規使用單引號和雙引號之間的區別。

0

而不是嘗試使用new File(path)得到File參考,請使用彈簧ResourceLoader接口。該ApplicationContext實現了這個接口,所以你可以從控制器這樣對它的引用(例如):

class MyController implements ApplicationContextAware { 

    private ResourceLoader resourceLoader 

    void setApplicationContext(ApplicationContext applicationContext) { 
    resourceLoader = applicationContext 
    } 

    def someAction() { 
    String path = "classpath:/ReadyOrdersFor-${vendor.name}" 
    File csvFile = resourceLoader.getResource(path).file 
    } 
} 

我不是100%肯定高於path值是正確的,你可能需要刪除'/'