IT기술/Flex

struts2 file upload

dobbby 2008. 11. 17. 16:20
반응형

StrutsFileUpload.java
package upload;

import java.io.*;

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 caption;//The caption of the file entered by user
   
    public String execute() throws Exception {
     try {
      String fullFileName = "d:/upload/"+uploadFileName;
      File theFile = new File(fullFileName);
      //FileUtils.copyFile(upload, theFile);
      FileInputStream fis = new FileInputStream(upload);
      FileOutputStream fos = new FileOutputStream(theFile);
      byte[] buf = new byte[1024];
      int n = 0;
      while((n=fis.read(buf, 0, buf.length))!=-1){
       fos.write(buf, 0, n);
      }
      fis.close();
      fos.close();
     }
     catch (Exception e) {
      addActionError(e.getMessage());
      return INPUT;
     }
     return SUCCESS;
    }
    public String getCaption() {
     return caption;
    }
    public void setCaption(String caption) {
     this.caption = caption;
 }
 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;
 }
}


struts.xml 내부 package
 <package name="upload" namespace="/upload" extends="struts-default">
 <!-- File Upload -->
  <action name="upload">
   <result>/upload/upload.jsp</result>
  </action>
  
  <action name="upload" class="upload.StrutsFileUpload">
   <result name="input">/upload/upload.jsp</result>
   <result>/upload/upload-success.jsp</result>
  </action>
 <!-- End File Upload -->
 </package>


upload.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>File Upload Example</title>
</head>

<body>

<s:actionerror />
<s:fielderror />
<s:form action="upload" 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>


upload-success.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Showcase</title></head>

<body>
<table>
 <tr>
 <td colspan="2"><h1>File Upload Example</h1></td>
 </tr>
 
 <tr>
 <td><label for="doUpload_upload">Content Type:</label></td>
 <td><s:property value="uploadContentType" /></td>
 </tr>
 
 <tr>
 <td><label for="doUpload_upload">File Name:</label></td>
 <td ><s:property value="uploadFileName" /></td>
 </tr>
 
 
 <tr>
 <td><label for="doUpload_upload">File:</label></td>
 <td><s:property value="upload" /></td>
 </tr>
 
 <tr>
 <td><label for="doUpload_upload">File Caption:</label></td>
 <td><s:property value="caption" /></td>
 </tr>

</table>

</body>
</html>

반응형