【问题标题】:How to upload a file(txt,pdf) and show it in the same webpage?如何上传文件(txt,pdf)并在同一个网页中显示?
【发布时间】:2016-05-18 05:31:36
【问题描述】:

我正在编写一个逻辑,当用户第一次访问网页时(例如在他的个人资料页面中),用户会上传一个文本文件。下次用户访问该网页时,他应该能够找到上传的文件作为链接。

到目前为止,我已经能够在用户特定的路径中上传文件,并考虑如何在网页中显示它。

我正在关注mkyong,但我在这里发现的问题是动作结果在流中,而在我的情况下,valuestack 有其他动作细节。

代码片段:

1.动作类

public String uploadResume(){

    try{

        if(uploadFile==null){
            throw new NullPointerException();
        }

    File file = new File("/Users/shibasish/Documents/","/" 
                    + GenerateTimestampID.generateTimestamp()+"/shibasish");
    if (!file.exists()) {
        if (file.mkdirs()) {
            System.out.println("Directory is created!");
        } else {
            System.out.println("Failed to create directory!");
        }
    }
    String filePath = file.getPath();
    File fileToCreate = new File(filePath, uploadFileFileName);
    FileUtils.copyFile(uploadFile, fileToCreate);       

    }catch(NullPointerException e){
        addActionError("Please upload a resume");
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return "success";
}

2.struts.xml

<action name="uploadResume" class="com.msventure.web.actions.CompleteProfileAction" 
              method="uploadResume"> 
    <interceptor-ref name="defaultStack">
    <param name="fileUpload.allowedTypes">
        text/plain,application/pdf,application/octet-stream
    </param>
</interceptor-ref> 
        <result name="success">/profile.jsp</result>
        <result name="fail">/login.jsp</result>
        <result name="index">/index.jsp</result>
          <result name="login">/talent.jsp</result>
    </action>

JSP

<form id="login" name="login" method="post" action="signup">
<p>
    <input type="submit" value="Update" />
</p>
  </form> 

  <s:if test="uploadFile neq null">
  <!--<h4>Download file - <s:a href="uploadFile">fileABC.txt</s:a></h4>-->
  <a href="<s:property value="uploadFile" />" />
  </s:if>
  <s:form id="login" name="login" method="post" action="uploadResume" 
       enctype="multipart/form-data">
    <s:file name="uploadFile" label="Select a File to upload" size="40"/>
    <s:submit value="submit" name="submit"/>
    <!--<input type="button" value="Search" id="resumeupload" />-->
</s:form>

如果我不清楚,请告诉我。

【问题讨论】:

    标签: java html jsp file-upload struts2


    【解决方案1】:

    您不能同时返回 JSP 页面二进制文件。

    返回 JSP 时,使用 dispatcher 结果,返回文件时,使用 stream 结果。

    上传文件后,您有两个选择:

    1。只显示文件

    使用target="_blank" 在新选项卡中发布表单并返回stream 结果;用户最终将拥有两个选项卡,一个带有配置文件,另一个带有文档。

    2。显示嵌入文件的 JSP

    在个人资料页面中使用&lt;iframe&gt;,并使用target="name_of_the_frame_here" 发布针对iframe 的表单。

    还有更复杂的方法(返回一个 JSP,并在服务器端组成一个 id,您将在 iframe 调用的辅助操作中使用它来获取文档),但我会选择解决方案 #2

    【讨论】:

    • 这是您建议的最好和最简单的方法。我使用了解决方案 #2。非常感谢。
    【解决方案2】:

    尽管 Andrea 提供了最好的方法,但我正在上传我使用 iframe 使用的代码,并在重新登录时下载用户特定的文件。 我希望这会有所帮助。

    1.JSP:这是iframe里面的JSP

    <s:form id="login" name="login" method="post" action="uploadResume" 
                                                 enctype="multipart/form-data">
        <s:file name="uploadFile" label="Select a File to upload" size="40"/>
        <s:submit value="submit" name="submit"/>
    </s:form><a href="downloadResume">Download file</a>
    

    2.操作方法:上传简历后,持久化用户特定路径。我已经使用cookies进行用户识别。

    public String downloadResume() {
    
        try {
    
            Cookie[] currentCookie;
            String usrCookie = "sample";
            String cookieFlg;
            currentCookie = request.getCookies();
    
            for (int i = 0; i < currentCookie.length; i++) {
                if (currentCookie[i].getName().equals("_usr")) {
                    usrCookie = currentCookie[i].getValue();
                }
            }
    
            if (!usrCookie.equalsIgnoreCase("sample")) {
    
                userProfileObj = new UserProfile();
    
                String pathloc=userProfileObj.getResumePath(usrCookie);
    
                if(pathloc==null){
                    //throw new NullPointerException();
                    addActionError("Please upload a resume");
                    return "fail";
                }
                else{
                File folder = new File(pathloc);
                File[] listFile=folder.listFiles();
                File fileToDownload=new File(pathloc+"/"+listFile[1].getName());
                for(int i=0;i<listFile.length;i++){
                    System.out.println("list of files : "+listFile[i].getName());
                }
                //System.out.print("Files in the directory : "+fileToDownload);
    
                inputStream = new FileInputStream(fileToDownload);
                fileName = fileToDownload.getName();
                Long contentLength = fileToDownload.length();
                }
            }
    
        } catch (NullPointerException e) {
            e.printStackTrace();
            addActionError("Please upload a resume");
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
    

    3.struts.xml

    <action name="downloadResume" class="com.msventure.web.actions.CompleteProfileAction" 
                                 method="downloadResume">
            <result name="success" type="stream">
                <param name="contentType">application/octet-stream</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="bufferSize">1024</param>
            </result>
            <result name="fail">/filemanagement.jsp</result>
        </action>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      相关资源
      最近更新 更多