fileupload实现

<html>
  <head></head>
  <body>
	<s:fielderror />
    		<s:form action="FileUploadAction" method="post" enctype="multipart/form-data">
  		<s:file name="file" label="File" />
  		<s:submit/>
  	</s:form>
  </body>
</html>
<action name="FileUploadAction" class="fileUploadAction">
	<!-- 配置 fileupload 的拦截器 -->
	<interceptor-ref name="fileUpload">
		<!-- 可以支持上传的文件类型 -->
		<param name="allowedTypes">
		image/bmp,image/png,image/gif,image/jpeg,image/jpg  
		</param>  
		<!-- 设置上传文件的大小不能超过1M -->  
		<param name="maximumSize">1024*1024</param>  
	</interceptor-ref>  
	<interceptor-ref name ="defaultStack" />
			
	<param name="savePath">/upload</param>
	<result name="success">/successFileUpload.jsp</result>
	<result name="input">/fileUpload.jsp</result>
</action>
import java.io.File;
import java.io.FileInputStream;  
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;  
import java.io.OutputStream;  

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("all")
@Scope("prototype")
@Controller("fileUploadAction")
public class FileUploadAction extends ActionSupport {
	
	@Override
	public String execute() throws Exception {
		
		if (file != null) {
			
			InputStream in = null;
			OutputStream out = null;
			try {
				
				in = new BufferedInputStream(new FileInputStream(getFile()));
				out = new BufferedOutputStream(new FileOutputStream(getSavePath() + "//" + getFileFileName()));
				byte [] buff = new byte[1024];
				int len = 0;  
				while ((len = in.read(buff)) > 0) {
					out.write(buff, 0, len);
				}
				out.flush();
			} catch(Exception ex) {
				ex.printStackTrace();
			} finally {
				in.close();
				out.close();
			}
			
			return SUCCESS;
		}
		
		return INPUT;
	}
	
	private File file;
	private String fileFileName;
	private String fileContentType;
	private String savePath;
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}  

}
import java.io.File;
import java.io.FileInputStream;  
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;  
import java.io.OutputStream;  

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("all")
@Scope("prototype")
@Controller("fileUploadAction")
public class FileUploadAction extends ActionSupport {
	
	@Override
	public String execute() throws Exception {
		
		if (file != null) {
			
			InputStream in = null;
			OutputStream out = null;
			try {
				
				in = new BufferedInputStream(new FileInputStream(getFile()));
				out = new BufferedOutputStream(new FileOutputStream(getSavePath() + "//" + getFileFileName()));
				byte [] buff = new byte[1024];
				int len = 0;  
				while ((len = in.read(buff)) > 0) {
					out.write(buff, 0, len);
				}
				out.flush();
			} catch(Exception ex) {
				ex.printStackTrace();
			} finally {
				in.close();
				out.close();
			}
			
			return SUCCESS;
		}
		
		return INPUT;
	}
	
	private File file;
	private String fileFileName;
	private String fileContentType;
	private String savePath;
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}  

}

猜你喜欢

转载自jaesonchen.iteye.com/blog/2287183