首页>代码>java struts2结合swfupload实现上传文件的demo教程>/struts2SwfUpload2/src/com/action/FileUploadAction.java
package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
	private File file;
	private String fileFileName;
	private String fileContentType;
	private String savePath;
	
	private String fileName;

	public String execute() throws Exception {
		
		//附件名称
		fileName = URLDecoder.decode(fileName, "UTF-8");  
				
		InputStream is = new FileInputStream(file);
		String root = getSavePath();
	
		File deskFile = new File(root, this.getFileFileName());
		OutputStream os = new FileOutputStream(deskFile);
		byte[] bytefer = new byte[1024];
		int length = 0;
		while ((length = is.read(bytefer)) != -1) {
			os.write(bytefer, 0, length);
		}
		os.close();
		is.close();
		
		//返回页面信息
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        
        //
        out.write("action返回的值:"+System.currentTimeMillis());
        
        out.flush();
        out.close();
        
		return "success";
	}

	
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}


	public void setSavePath(String savePath) {
		this.savePath = 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 String getFileName() {
		return fileName;
	}


	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

}
最近下载更多
lyd19931203  LV21 2020年6月16日
丶右转  LV11 2020年6月1日
skipple3  LV39 2020年3月3日
15094087186  LV15 2019年10月9日
dfaghrs  LV19 2019年5月24日
三生石sh1  LV13 2019年2月26日
秋天1829698  LV1 2019年1月11日
lz1562094  LV1 2018年12月4日
sunforever  LV1 2018年9月7日
austinChan  LV3 2018年8月20日
最近浏览更多
czr2233  LV9 2021年9月14日
小张 2021年5月17日
暂无贡献等级
xuexizhuanyong23  LV16 2021年3月2日
qqwjk123  LV2 2021年1月10日
朝鲜飞行员  LV7 2020年11月20日
gnnhka  LV10 2020年9月17日
lyd19931203  LV21 2020年6月16日
ankangyong  LV1 2020年6月2日
丶右转  LV11 2020年6月1日
pt11100  LV9 2020年5月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友