文件上传action

public String upload(){
		HttpSession session=ActionContext.getActionContext().getMap().getSession();
		String filepath=session.getServletContext().getRealPath("/")+"image/";
		File f=new File(filepath);
		if(!f.exists()){
			f.mkdir();
		}
		f=new File(filepath+"/"+System.currentTimeMillis()+".jpg");
		try {
			FileInputStream fis=new FileInputStream(file);
			FileOutputStream fos=new FileOutputStream(f);
			byte [] data=new byte[1024];
			while((fis.read(data, 0, 1024))!=-1){
				fos.write(data, 0, 1024);
			}
			fos.flush();
			fos.close();
			fis.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "add";
	}

 

你可能感兴趣的:(action)