我认为我有一个上传表格:
<input type="file" name="file" value="search file" /><br />
在我的 Controller 中,我像这样加载它:
def file = request.getFile('file')
def f = file.getInputStream()
def input = f.getText()
因此,现在有了一个名为input的字符串,其中包含文件的内容。
我想要UTF-8。这怎么可能 ?
编辑:
我的问题是,要上传的文件位于“Windows-1252”中,而像äöü这样的德语字符现在在名为“input”的字符串中有所不同。
如果我在UTF-8中使用“Notepad ++”转换文件,然后将其上传,则可以使用。但是我每次都做不到。
编辑2:
def file = request.getFile('file') //get file from view
def File tmpfile = new File('C:/tmp/tmpfile.txt') //create temporary file
file.transferTo(tmpfile) //copy into tmpfile
CharsetToolkit toolkit = new CharsetToolkit(tmpfile) //toolkit with tmpfile
def charset = toolkit.getCharset() //save charset in a variable
def input = tmpfile.getText(charset) //get text with right charset
我尝试了一些不同的文档。但是变量charset总是
UTF_8
最佳答案
您可以使用 getText(String charset)
def input = f.getText('UTF-8')
关于grails - 用grails更改文件的编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21408288/