【问题标题】:Display text value in a form or a textArea, textForm by GSP and Groovy code通过 GSP 和 Groovy 代码在表单或 textArea、textForm 中显示文本值
【发布时间】:2015-02-14 12:30:14
【问题描述】:

所以我有一个uploadForm,在我选择要上传的文件并提交之后,我想将文件文档的内容显示到textArea或Form,但我不知道如何获取该文件的参数和在该文件中呈现文本。
这是我的代码:文件displayForm.gsp

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Upload</title>
</head>
<body>
<div class="body">
</div>
<g:uploadForm action ="uploaded" >
    <input type = "file" name = "cfile" value = "File"/>
    <g:actionSubmit  action="displayForm" value = "submit" />
</g:uploadForm>
    <p>
        <textField rows="50" cols="100" value = "${cfile}"
        name="displayResult"/ >          
    </p>
    <input type="button" value = "Display" name = "Display">
    <br />
</body>
</html>  

这是文件SampleController.groovy

class SampleController{
def f
def displayForm() {
    if(params.cfile){
        if(params.cfile instanceof        org.springframework.web.multipart.commons.CommonsMultipartFile){                  //new FileOutputStream('D:/submitted_file.txt').leftShift( params.cfile.getInputStream() );
            f=params.cfile.transferTo(new File('D:/submitted_file.uima'));
            render file:params.cfile.toString() ,contentType: "text", encoding:"UTF-8"  
        }else{
            log.error("wrong attachment type [${cfile.getClass()}]");
        }
    }
}

正如您在上面看到的,我不想读取文件'D:/submitted_file.uima' 并渲染它。
但是如果我调用渲染方法,则会出现文件为空的错误。

我在从文件.gsp 调用文件参数时遇到问题。我应该从uploadForm 和textArea(textField) 调用名称“cfile”或“value”或“id”或其他名称。

【问题讨论】:

    标签: java grails web groovy gsp


    【解决方案1】:

    render file:x 方法接受byte[]java.io.FileinputStream 参数,而您似乎在传递CommonsMultipartFile。尝试在上面的代码中调用render file: f, ...,来实际渲染你保存到磁盘的文件。

    【讨论】:

      【解决方案2】:

      如果此文件不存在,您必须创建此文件。

      File file = new File("C:/test.txt");
      if(!file.exists()) {
         file.createNewFile();
      }
      

      【讨论】:

      • 这不是真的,transferTo 的文档中明确指出如果目标文件已经存在,则会先将其删除。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 2012-05-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多