Java 文件流

 

private File file;  	


@Action(value = "upload_photo")
	public String upload_photo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		UserID = Integer.parseInt(request.getParameter("userid"));    
		try{
			
		if (file == null || UserID == null) {
			 return "success";
		} 
			String[] c = new SimpleDateFormat("yyyy,MM,dd").format(new Date())
					.split(",");
			StringBuffer sb = new StringBuffer();
			for (String temp : c) {
				sb.append(temp + "/");
			}
			File dir = new File(this.imgFileStore.getRootFile().getPath()  //路徑
					+ "/photo/" + UserID);
			if (!dir.exists()) {
				dir.mkdirs();
			}
		 
			  String newfilename = new Date().getTime() +
			  fileFileName.substring(fileFileName.lastIndexOf("."));// 獲取時間戳 			  
			
			//String newfilename = fileFileName;// 獲取時間戳
			File new_File = new File(dir, newfilename);
			FileInputStream fis = new FileInputStream(file);
			FileOutputStream fos = new FileOutputStream(new_File);

			byte[] b = new byte[512]; // 修改文件名
			int a = 0;
			while ((a = fis.read(b)) != -1) {
				fos.write(b, 0, a);
			}
			fis.close();
			fos.close();
			String sql = "update pub_user set PhotoName='photo/" + UserID
					+ "/" + newfilename + "' where user_id=" + UserID;
			jdbc.update(sql); 
		} catch (Exception e) { 
		
		} 
		 return "success";		
	}
相關文章
相關標籤/搜索