Flex Struts2 fileupload上传文件

	file=new FileReference();
	file.addEventListener(Event.SELECT,onSelect);
	file.addEventListener(ProgressEvent.PROGRESS,onProgess);

//上传附件
public function upload():void{
	var allTypes:FileFilter=new FileFilter("所有文件(*.*)","*.*");
	file.browse([allTypes]);
}
//选择文件
public function onSelect(event:Event):void{
	trace(file.size);
	if(file.size<=4*1024*1024){
		var request: URLRequest = new URLRequest(serverURL+"/UploadY!fileUpload.action"); 
		var vars:URLVariables = new URLVariables();//向后台传输的变量
		vars.FileName = file.name;  //文件名称
		vars.FileType=file.type;//文件类型
		vars.S_USER_ID=userInfo.S_USER_ID;//用户ID	
		//上传文件名
		var date:Date=new Date();
		var uploadFileName:String=date.fullYear.toString()+(date.month+1).toString()+date.date.toString()+date.hours.toString()+date.minutes.toString()+date.seconds.toString()+Math.round(Math.random()*100000000000000);
		vars.uploadFileName=uploadFileName;
	
		request.data = vars;//向后台请求传输的参数
		request.method = URLRequestMethod.POST;//POST
		try{
			file.upload(request,"upload");
			var fileObject:Object=new  Object();
			fileObject.FileName=file.name;
			fileObject.UpLoadName=uploadFileName+file.type;
			fileArray.addItem(fileObject);
			
		//	fileArray.refresh();
		}catch(e:ExpectException){
			Alert.show("上传失败!","提示");
		}
	}else{
		Alert.show("文件超过4M","提示");
	}
}
//文件进度
public function onProgess(event:ProgressEvent):void{
	trace(event.bytesLoaded);
	trace(event.bytesTotal);
	var proc:uint=event.bytesLoaded/event.bytesTotal*100;
	trace(proc);
}




package com.fcx.app.faq.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
import org.apache.tomcat.jni.OS;

import com.fcx.app.core.action.DefaultComplexAction;
import com.fcx.app.core.service.GenericService;

public class UploadAction extends DefaultComplexAction {
	private static final long serialVersionUID = 4459273642763849146L;

	private static final String[] String = null;
	
	private File upload;//定义的文件
	private String uploadFileName;//上传文件的名称
	private String savePath ;//上传得路径
	private String fileEncodings;//读入文件编码类型
	private String  filename;//前台传过来的参数
	private String S_USER_NAME;//前台传入的用户名
	private String FileTyep;
	
	public UploadAction(GenericService service) {
		super(service);
		// TODO Auto-generated constructor stub
	}
	public String fileUpload(){
				//获取服务器路径
				savePath = request.getRealPath("/");
				//ServletActionContext获得前台传过来的参数
				Map map = ServletActionContext.getRequest().getParameterMap();
				//获取前台传入的文件名
				String[] dir = (String[])map.get("FileName");
				String filename = dir[0];
				//创建上传文件名
				dir=(String[])map.get("uploadFileName");
				String uploadFileName=dir[0];
				//String uploadFileName=UUID.randomUUID().toString();
				//uploadFileName=uploadFileName.substring(0,8)+uploadFileName.substring(9,13)+uploadFileName.substring(14,18)+uploadFileName.substring(19,23)+uploadFileName.substring(24); 
				//获取用户ID
				dir = (String[])map.get("S_USER_ID");
				String S_USER_NAME= dir[0];
				//获取文件类型
				dir= (String[])map.get("FileType");
				FileTyep=dir[0];
				//创建上传路径
				savePath+="\\WEB-INF\\"+S_USER_NAME;
			    InputStream is;
			    OutputStream os;
				try {
					 //创建输入流
					 is = new FileInputStream(upload);
					 //创建文件
					 File tempFile=new File(savePath,uploadFileName+FileTyep);
					 //判断路径是否存在
					 if(!tempFile.getParentFile().exists()){
						 //创建路径
						 tempFile.getParentFile().mkdirs();
					 }
					 //判断文件是否存在
					 if(!tempFile.exists()){
						 	//创建文件
							tempFile.createNewFile();
						}
					 	//创建输出流
			            os=new FileOutputStream(tempFile);
			            byte[] buf=new byte[1024];
			            int length=0;
			            while((length=is.read(buf))>0)
			            {
			            	//写入
			                os.write(buf, 0, length);
			            }
			            is.close();
			            os.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		return SUCCESS;
	}
	public String deleteFile(){
		//获取服务器路径
		savePath = request.getRealPath("/");
		//获取上传文件名
		String uploadFileName=queryParams.get("UpLoadName");
		String S_USER_NAME=queryParams.get("S_USER_ID");
		//获取文件路径
		savePath+="\\WEB-INF\\"+S_USER_NAME;
		File file=new File(savePath,uploadFileName);   
		boolean tf = false;
	        if(file.exists()){   
	            tf= file.delete();
	        }   
	        System.out.println(tf);
		return SUCCESS;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getFileEncodings() {
		return fileEncodings;
	}

	public void setFileEncodings(String fileEncodings) {
		this.fileEncodings = fileEncodings;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getS_USER_NAME() {
		return S_USER_NAME;
	}
	public void setS_USER_NAME(String s_USER_NAME) {
		S_USER_NAME = s_USER_NAME;
	}
	public String getFileTyep() {
		return FileTyep;
	}
	public void setFileTyep(String fileTyep) {
		FileTyep = fileTyep;
	}

}


你可能感兴趣的:(fileupload)