May 2009 Archives

I forgot where this code appears from?

Server side:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;

public class ImageService {
public void uploadImage(String productId, DataHandler image) {
System.out.println(image.getContentType());
try {
InputStream in = image.getInputStream();
String imageDir = "c:/tmp";
FileOutputStream out = new FileOutputStream(new File(imageDir,
productId));
try {
byte buf[] = new byte[1024];
for (;;) {
int noBytesRead = in.read(buf);
out.write(buf, 0, noBytesRead);
if (noBytesRead < buf.length) {
break;
}
}
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Client side:

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

public class ImageServiceClient {

/**
* @param args
*/
public static void main(String[] args) {
ImageServiceService service = new ImageServiceService();
/*service._getServiceClient().getOptions().setProperty(
Constants.Configuration.ENABLE_MTOM, "true");*/
ImageServiceDelegate delegate = service.getImageServicePort();
DataSource source = new FileDataSource("c:/90325-232111.jpg");
DataHandler handler = new DataHandler(source);
ByteArrayOutputStream buffOS= new ByteArrayOutputStream();
try {
handler.writeTo(buffOS);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buff = buffOS.toByteArray();
delegate.uploadImage("p01.jpg", buff);
System.out.println("Done!");
}

}

Ext Js & RESTful file upload

| No TrackBacks

The buweb site must be changed into a Web 2.0 style website. So, I found out the RESTful file upload example, Multiple file upload using RESTful web service (Jersey) , and used EXt Js file upload to build my client upload page.

But must care some details, like "missing } in XML expression" on FireFox. If server side use "Content-type:text/plain", the respone JSON will add <pre><pre> tags on FireFox, so change to "text/html".

Second, the Ext Js file upload example doesn't have submit failure responses, like client invalid, server invalid, connect failure and load failure. The formsubmit-example has complete example code.

Then, usually need to do field validation, use vtype to check file extension format. Ex: ExtJS中表单验证使用自定义vtype示例.

Cpanel Fullbackup script

| No TrackBacks

From: TotalChoice Hosting General Support

You can cron a job and run the below script.

30 3 * * * /usr/local/bin/php /home/youraccount/fullbackup.php

// PHP script to allow periodic cPanel backups automatically.
// Based on script posted by max.hedroom in cpanel.net forums
// This script contains passwords. KEEP ACCESS TO THIS FILE SECURE!

// ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********

// Info required for cPanel access
$cpuser = "username"; // Username used to login to CPanel
$cppass = "password"; // Password used to login to CPanel
$domain = "example.com"; // Domain name where CPanel is run
$skin = "monsoon"; // Set to cPanel skin you use (script won't work if it doesn't match)

// Info required for FTP host
$ftpuser = "ftpusername"; // Username for FTP account
$ftppass = "ftppassword"; // Password for FTP account
$ftphost = "ftp.example.com"; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)

// Notification information
$notifyemail = "you@example.com"; // Email address to send results

// Secure or non-secure mode
$secure = 0; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP

// Set to 1 to have web page result appear in your cron log
$debug = 0;

// *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********

if ($secure) {
$url = "ssl://".$domain;
$port = 2083;
} else {
$url = $domain;
$port = 2082;
}

$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection... Bailing out!\n"; exit; }

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";

// Make POST to cPanel
fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {
$response = fgets($socket,4096);
if ($debug) echo $response;
}

fclose($socket);

?>

Expand VMware Virtual Disk

| No TrackBacks

First, check your vmdk type. If the disk was splited in 2GB files, suggest convert to a single virtual disk. Ex: vmware-vdiskmanager -r sourceDisk.vmdk -t 0 destinationDisk.vmdk
Step 1, Expand Virtual Disk. Ex: vmware-vdiskmanager -x 6GB myDisk.vmdk
Step 2, Boot up Guest OS, WinXP, use Diskpart to merge two partitions. If this disk is system disk, you need copy vmdk to the other. Then mount the other vmdk file and merge its' partitions.

regedit_ss.gif
By PHM Registry Editor.
Find out [HKEY_LOCAL_MACHINE\Comm\"YOUR WLAN DEVICE"\Parms], there have "NetworkAddress" string, just modify it, if not need to add "NetworkAddress" string and type you want the Mac Address.

PHP and Ext Js with JSON

| No TrackBacks

之前的PHP如何讀取Excel檔,提供一個Excel檔只要按下資料更新,但是如果沒有安裝Excel軟體的電腦,就沒法分析這些權証的投資價值,例如:排序等等功能。
所以想利用Ext Js的Grid來顯示,便可達成在Browser上直接排序的需求。而成果如下:
Ext Js版的最新權証列表
在這實作的步驟為:
1.擷取要顯示的資料,並將資料轉成JSON。可參考Tutorial:Creating JSON Data in PHP。必須要特別注意的是,json_encode($array),如果$array的Key不是連貫的,那轉出來的JSON資料會有其KEY,{KEY,{$array[0]...$array[n]}},而不是所需的格式,{$array[0]...$array[n]},這點需要特別小心。
2.撰寫網頁及JavaScript,使用Ext Js的Library,可參考JSON Grid Example
3.由於每次點那PHP網頁,都要重新抓取檔案分析內容,所以視資料更新情況而定,其實可以透過排程,只要執行一次將結果轉出成靜態的HTML檔案,既可節省Server負擔,還可以縮短使用者等待時間。
更進階的方法,將資料分頁,可參考ExtJS tutorial part III: Paging Grid with PHP and MySql

延伸閱讀:
Tutorial:Ext20 Grid Editor PHP MySQL
Tutorial:如何建立一個可以Disable/Editable的Editor gird
Complex Data Binding by Saki

March 2010

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Archives

Powered by Movable Type 4.34-en

Recent Comments

  • panandy: 3Q read more
  • Anonymous: philipz大 沒有邀請碼,無法註冊,致無法下載工具 請問有何方法可以解決? 感恩 read more
  • philipz: harushard, It is your java code problem. Check it and read more
  • harushard: java.lang.NoClassDefFoundError: Could not initialize class examples.ExamplesMCRFactory could you explain why read more
  • philipz: IM已經是無解了,建議可以用Plurk Api試看看。 read more
  • ann: hi,請問以上問題解決了沒?可以分享一下嗎? read more
  • Anonymous: 邀請碼沒有~無法下載工具~~怎麼辦 read more
  • Melody: test read more
  • philipz: Ginger: 您好,請您在我的噗浪上留言即可一起討論。 read more
  • Ginger: 版大您好: 在看過您做的期貨即時訊息BOT後覺得很不錯,自己也有一些想法不知能否與您交流與協助呢? 若您覺得ok的話,可以email或是噗浪(ID: king0355)與我聯繫。感謝 : ) read more

About this Archive

This page is an archive of entries from May 2009 listed from newest to oldest.

April 2009 is the previous archive.

June 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.