首页>代码>struts2开发文件上传下载的简单实例>/uplode/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 org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

	private File file;
	private String fileFileName;
	//提交过来的file的MIME类型
    private String fileContentType;
    
    @Override
    public String execute() throws Exception {
    	String root=ServletActionContext.getServletContext().getRealPath("/file");
    	InputStream is=new FileInputStream(file);
    	OutputStream os = new FileOutputStream(new File(root, fileFileName));
    	System.out.println("fileFileName"+fileFileName);
    	// 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同
        System.out.println("file: " + file.getName());
        System.out.println("file: " + file.getPath());
        byte[] buffer=new byte[500];
        int length=0;
        while(-1 !=(length=is.read(buffer,0,buffer.length))){
        	os.write(buffer);
        }
        os.close();
        is.close();
    	return SUCCESS;
    }
    
	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;
	}
}
最近下载更多
酒非儿  LV6 2021年12月3日
bcr1234  LV9 2020年8月6日
cz206616  LV10 2020年5月29日
王可以  LV10 2020年4月22日
oldtrybest  LV8 2019年8月29日
11ccvvbb  LV6 2019年4月10日
wdlsuper  LV4 2019年1月24日
zzzxxx32  LV3 2019年1月8日
斤斤计较jh  LV1 2018年12月9日
chengqiang  LV13 2018年6月29日
最近浏览更多
lironggang  LV38 2022年11月20日
uni-code_0123  LV1 2022年11月16日
雷迪斯俺的乡亲们  LV11 2022年3月9日
zhangmingzuidaima  LV3 2021年12月16日
酒非儿  LV6 2021年12月3日
dongzhan  LV12 2021年11月27日
sl0018  LV13 2021年10月3日
ALT_LB  LV4 2021年8月23日
isMrQt  LV1 2021年7月15日
MrQian  LV1 2021年6月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友