星期四, 7月 28, 2011

[C#] HttpWebRequest request limitation

最近實作多執行緒上傳檔案的測試工具時,
發現不管執行幾個執行緒同時只會有二個連線數存在。
這是.net預設的連線數,透過以下設定即可。
ServicePointManager.DefaultConnectionLimit = 10;
網路上有人建議最大連線數不要超過1024,最好的效果是512。
另外也可以從app.config來設定最大連線數

硬碟單位換算


單位換算問題:適用於各語言的實作XD
10 MB = 10485760 Bytes

Explanation: 10 MB = 10*1024 KB = 10*1024*1024 Bytes = 10485760 Bytes

As we have,

1 KB = 1024 Bytes

1 MB = 1024 KB


Read more: 

[Java] Resize Image as JPG

簡單的影像Resize程式

public class ResizeImage {

 static public byte[] resizeImageAsJPG(byte[] pImageData, int pMaxWidth)
   throws IOException {

  // Create an ImageIcon from the input image byte[]
  ImageIcon imageIcon = new ImageIcon(pImageData);
  int width = imageIcon.getIconWidth();
  int height = imageIcon.getIconHeight();

  // If the image is larger than the max width, we need to resize it
  if (pMaxWidth > 0 && width > pMaxWidth) {
   // Determine the shrink ratio
   double ratio = (double) pMaxWidth / imageIcon.getIconWidth();
   height = (int) (imageIcon.getIconHeight() * ratio);
   width = pMaxWidth;
  }

  // Create a new empty image buffer to "draw" the resized image into
  BufferedImage bufferedResizedImage = new BufferedImage(width, height,BufferedImage.SCALE_SMOOTH);
  // Create a Graphics object to do the "drawing"
  Graphics2D g2d = bufferedResizedImage.createGraphics();
  g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  // Draw the resized image
  g2d.drawImage(imageIcon.getImage(), 0, 0, width, height, null);
  g2d.dispose();
  // Now our buffered image is ready
  // Encode it as a JPEG
  ByteArrayOutputStream encoderOutputStream = new ByteArrayOutputStream();
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(encoderOutputStream);
  encoder.encode(bufferedResizedImage);
  byte[] resizedImageByteArray = encoderOutputStream.toByteArray();
  return resizedImageByteArray;
 }
}

[jQuery] File upload using AJAX form

最近使用jQuery Form Plug & jQuery File Upload上傳檔案,如果將Server Response的content-type設定application/json的話,因為元件上傳使用iframe來達到ajax form的效果,因此在Firefox和IE瀏覽器會跳出下載的Dialog。如果要回傳json格式來交換資料的話,只能將content-type設定text/plain或text/html,等接到ajax callback事件後,再將json的字串利用JSON.parser()轉換到json object即可。


後紀:使用text/plain會被多加pre element進去

Reference:Frequently Asked Questions
Internet Explorer prompting to download a file after the upload completes

The file upload plugin makes use of an Iframe Transport module for browsers like Microsoft Internet Explorer and Opera, which do not yet supportXHR file uploads.
Iframe based uploads require a Content-type of text/plain or text/html for the JSON response - they will show an undesired download dialog if the iframe response is set to application/json.
Please have a look at the Content-Type Negotiation section of the Setup instructions.

星期五, 7月 15, 2011

[C#] Settings

To Write and Persist User Settings at Run Time
  1. Access the user setting and assign it a new value, as shown in the following example:
    Properties.Settings.Default.myColor = Color.AliceBlue;
    
  2. If you want to persist changes to user settings between application sessions, call the Savemethod, as shown in the following code:
    Properties.Settings.Default.Save();

[C#] 實作 Multipart/Form-data 上傳檔案

以下範例只能支援小檔案,大檔會把StringBuilder 撐爆,需加以改良
可參考前一篇文章的教學:[JAVA] Simulator to POST multipart/form-data using HttpURLConnection
其實整個POST流程只要了解用任何語言都很好實作:)

[Java] 實作HTTPS Request

最近的專案為了確保網路資料傳輸的安全性,使用了SSL來加密,
原本實作的RESTful Client(Java HttpsURLConnection)模擬request時,伺服器回應了以下錯誤:

javax.servlet.ServletException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:418)
 com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
 com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Google了一下找到了這篇文章:

星期二, 7月 05, 2011

[Fedora] Fedora 指令集

切換root帳號
#su

renew ip
#dhclient -r

重新分配網路介面卡ip
#dhclient eth5

星期一, 7月 04, 2011

[jQuery API] How to post JSON data to Server 如何送json格式到server

If you want to post JSON data to server using ajax() method, processData must be set to false.

Tips:requestBODY is JSON string.