IT기술/Flex

Struts 2 File Upload

dobbby 2008. 11. 17. 09:40
반응형

http://www.roseindia.net/struts/struts2/struts-2-file-upload.shtml

Here is the full code of action class StrutsFileUpload.java
package net.roseindia;
import java.util.Date;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsFileUpload extends ActionSupport {
  
    private File upload;//The actual file
    private String uploadContentType; //The content type of the file
    private String uploadFileName; //The uploaded file name
  private String fileCaption;//The caption of the file entered by user
    public String execute() throws Exception {

    return SUCCESS;

    }
  public String getFileCaption() {
    return fileCaption;
  }
  public void setFileCaption(String fileCaption) {
    this.fileCaption = fileCaption;
  }
  public File getUpload() {
    return upload;
  }
  public void setUpload(File upload) {
    this.upload = upload;
  }
  public String getUploadContentType() {
    return uploadContentType;
  }
  public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
  }
  public String getUploadFileName() {
    return uploadFileName;
  }
  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }

  
}


Here we have not shown the code to save the uploaded file. But it can be done easily using the following code in execute(..) method of action class. Here is code snippet.

//Following code can be used to save the uploaded file

try {

String fullFileName = "c:/upload/myfile.txt";

File theFile = new File(fullFileName);

FileUtils.copyFile(upload, theFile);

} catch (Exception e) {

addActionError(e.getMessage());

return INPUT;

}

Writing JSP page

Here is the code of jsp file (upload.jsp) that displays the file upload form to the user.

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>File Upload Example</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/>

</head>

<body>

<s:actionerror />
<s:fielderror />
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
<tr>
<td colspan="2"><h1>File Upload Example</h1></td>
</tr>

<s:file name="upload" label="File"/>
<s:textfield name="caption" label="Caption"/>
<s:submit />
</s:form>
</body>
</html>

In the above code the form encrypt type is "multipart/form-data" and <s:file ../> tag renders the html file tag.

File upload success page

Here is the code of file upload(upload-success.jsp) success page.

<%@ page 
language="java" 
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/>
</head>

<body>
<table class="wwFormTable">
<tr>

<td colspan="2"><h1>File Upload Example</h1></td>
</tr>

<tr>
<td class="tdLabel"><label for="doUpload_upload" class="label">Content Type:</label></td>
<td><s:property value="uploadContentType" /></td>
</tr>

<tr>
<td class="tdLabel"><label for="doUpload_upload" class="label">File Name:</label></td>
<td ><s:property value="uploadFileName" /></td>
</tr>


<tr>
<td class="tdLabel"><label for="doUpload_upload" class="label">File:</label></td>
<td><s:property value="upload" /></td>
</tr>

<tr>
<td class="tdLabel"><label for="doUpload_upload" class="label">File Caption:</label></td>
<td><s:property value="caption" /></td>
</tr>


</table>

</body>
</html>

Adding mapping in struts.xml file

Add the following mapping in the struts.xml file.

<!-- File Upload -->

<action name="showUpload">
<result>/pages/upload.jsp</result>
</action>

<action name="doUpload" class="net.roseindia.StrutsFileUpload">
<result name="input">/pages/upload.jsp</result>
<result>/pages/upload-success.jsp</result>
</action>

<!-- End File Upload -->

The "showUpload" action displays the upload form and "doUpload" action actually uploads the file.

Running the example

To test the application compile code and then run the tomcat. Type http://localhost:8080/struts2tutorial/roseindia/showUpload.action in your browser. You browser should show the following form:

 

Now browse the file, enter caption and then click on the "Submit" button. Application will upload your file and then following success screen will be displayed.

 

There is one important point to be noted about File Upload Interceptor. The File Upload Interceptor actually deletes the the upload, once the action is executed. Here is the screen shot of tomcat that shows the file delete message:

INFO: Removing file upload C:\apache-tomcat-6.0.10Struts2\apache-tomcat-6.0.10\work\Catalina\
localhost\struts2tutorial\upload__13f532f7_1132e1d4754__8000_00000000.tmp

In this section you learnt the concept of file upload in struts 2.


반응형