【问题标题】:How to get uploaded file to local folder in spring surf java webscript?如何在spring surf java webscript中将上传的文件上传到本地文件夹?
【发布时间】:2013-10-30 10:41:26
【问题描述】:

我正在上传一个文件。我想获取文件并保存到我的本地系统。为此,我在 java 中使用 spring surf webscripts。谁能告诉我如何获取我的文件。

这是我的 ftl 文件:

<form name="frmUpload" id="frmUpload"action="${url.context}/upload"     enctype="multipart/form-data" method="get">
<input type="file" size="40" id="toBeUploaded" name="toBeUploaded" tabindex="2" onchange = "document.getElementById('frmUpload').submit()"required />
</form>

我正在创建一个支持的 java webscript 来获取这个文件。这是我的java代码。

public class Upload extends DeclarativeWebScript{

    protected ServiceRegistry serviceRegistry;
    private static final long serialVersionUID = 1L;
    private String fileName;
    private String filePath;

    private File toBeUploaded;  
    private String toBeUploadedFileName = "";  
    private String toBeUploadedContentType;  

    /** Multi-part form data, if provided */
    private FormData formData;

    /** Content read from the inputstream */
    private Content content = null;

    // upload settings
    private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB

      @Override
      protected Map executeImpl(WebScriptRequest req,Status status) {         
          System.out.println("backed webscript called");


          Boolean isMultipart  = false;


          String fileName  = req.getParameter("toBeUploaded");        
          if(fileName == null){
              System.out.println("File Name is null");
          }else{
              System.out.println("File Name is :" + fileName);
          }       


          HttpServletRequest request = ServletUtil.getRequest();
          String file = request.getParameter("toBeUploaded");
          File file2 = new File(file);
          String filePath = request.getSession().getServletContext().getRealPath("/");        
          File fileToCreate = new File(filePath, this.toBeUploadedFileName);          
          System.out.println("filepath "+filePath);

          try {
                FileUtils.copyFile(file2, fileToCreate);
                //validateBundle(fileToCreate);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
               System.out.println("filetocreate "+fileToCreate);


        }

}

文件名正常运行,但抛出 FileNotFoundExeption。这是堆栈跟踪

java.io.FileNotFoundException: Source 'test.jar' does not exist
        at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:637)
        at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:607)

【问题讨论】:

    标签: java alfresco surf alfresco-share spring-surf


    【解决方案1】:

    要获取上传的表单,您需要通过FormData 对象。您的代码将类似于:

        // Get our multipart form
        final ResourceBundle rb = getResources();
        final FormData form = (FormData)req.parseContent();
        if (form == null || !form.getIsMultiPart())
        {
            throw new ResourceBundleWebScriptException(Status.STATUS_BAD_REQUEST, rb, ERROR_BAD_FORM);
        }
    
        // Find the File Upload file, and process the contents
        boolean processed = false;
        for (FormData.FormField field : form.getFields())
        {
            if (field.getIsFile())
            {
                // Logic to process/save the file data here
                processUpload(
                        field.getInputStream(),
                        field.getFilename());
                processed = true;
                break;
            }
        }
    
        // Object if we didn't get a file
        if (!processed)
        {
            throw new ResourceBundleWebScriptException(Status.STATUS_BAD_REQUEST, rb, ERROR_NO_FILE);
        }
    

    如果您确定上传的字段名称,则可以将其中一些逻辑位短路

    【讨论】:

    • 感谢 gagravarr。你能告诉我这个 processUpload 方法是什么吗?
    • form.getIsMultiPart() 是假的。我已在表单中声明 enctype="multipart/form-data"。我已将此 webscript 配置为配置文件中的 bean。 bean定义是否需要更多配置,例如多部分表单的属性等?
    • processUpload 是您放置逻辑来处理文件的地方!您只需要发送带有结构的表单,使用诸如 firebird 或 wireshark 之类的东西来检查您发送的内容是否正确
    • 好的,但我无法获取我的文件。我搜索了很多。有人说它是 java 中的 surf webscripts 中的一个错误。请问你能帮帮我吗?
    • Alfresco 至少有一些这样做的例子,这就是我的答案中的代码主要来自哪里!我只能建议您尝试将您的代码和 webscript 与 Alfresco 中的工作进行比较
    猜你喜欢
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多