JavaTechie

Its all about Technology

Apache HTTPD subdomain configuration with Virtual Hosts on a local machine January 7, 2008

Filed under: Java — javatechie @ 5:51 am
Tags:

Open up your HOSTS file to add the changes to your system (so that Windows knows where to look when you ask for that domain):
Look for:

C:/WINNT/SYSTEM3/DRIVERS/ETC/HOSTS

and open up in your fav text editor.
Then add the new virtual hosts to the file (see my edited file below – note the differences) so that:

127.0.0.1 impistage would now become 127.0.0.1 abc.oneafrikan.com.

And that’s it really – fire up Apache again

Open up your Apache httpd.conf file, then go to:

C:Program Files/Apache Group/Apache/conf

and open up the file in your favourite text editor and go to the (usually) bottom of the file, and look for the bit about Virtual Hosts.

My local server Virtual Host used to have:

ServerName impistage with the DocumentRoot set to “C:/htstage” and the Directory set to “c:/htstage“.

My local Virtual Host now has it looking like:

ServerName staging.oneafrikan.com with the DocumentRoot set to C:/htstage/oneafrikan.com and the Directory set to C:/htstage/oneafrikan.com.

 

File upload using Java December 12, 2007

Filed under: Uncategorized — javatechie @ 11:24 am
Tags:

Checking If an HTTP Request is Encoded in Multipart Format

Now that you have installed the Apache Jakarta Commons FileUpload library, you can start writing the code. First, we have to make sure the HTTP request is encoded in multipart format. This can be done using the static method isMultipartContent() of the ServletFileUpload class of the org.apache.commons.fileupload.servlet package:

if (ServletFileUpload.isMultipartContent(request)){
// Parse the HTTP request…
}

In the above Java code snippet, request is a javax.servlet.http.HttpServletRequest object that encapsulates the HTTP request. It should be very familiar to you if you know Java Servlet or JSP.

Parsing Form Data with Java Servlet / JSP

Second, we will parse the form data contained in the HTTP request. Parsing the form data is very straightforward with the Apache Jakarta Commons FileUpload library:

ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
List fileItemsList = servletFileUpload.parseRequest(request);

(In the above Java code snippet, DiskFileItemFactory is a class contained in the org.apache.commons.fileupload.disk package and List is an interface contained in the java.util package.)

If everything works fine, fileItemsList will contain a list of file items that are instances of FileItem of the org.apache.commons.fileupload package. A file item may contain an uploaded file or a simple name-value pair of a form field. (More details about FileItem will be provided later.)

By default, the ServletFileUpload instance created by the above Java code uses the following values when parsing the HTTP request:

  • Size threshold = 10,240 bytes. If the size of a file item is smaller than the size threshold, it will be stored in the memory. Otherwise it will be stored in a temporary file on disk.
  • Maximum HTTP request body size = -1, which means the server will accept HTTP request bodies of any size.
  • Repository = System default temp directory, whose value can be found by the Java code System.getProperty(”java.io.tmpdir”). Temporary files will be stored there.

If you do not like the default settings, you can change them using the methods setSizeThreshold() and setRespository() of the DiskFileItemFactory class and the setSizeMax() method of the ServletFileUpload class, like this:

DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(40960); /* the unit is bytes */

File repositoryPath = new File(”/temp”);
diskFileItemFactory.setRepository(repositoryPath);

ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
servletFileUpload.setSizeMax(81920); /* the unit is bytes */

(In the above Java code snippet, File is a class of the java.io package.)

If the size of the HTTP request body exceeds the maximum you set, the SizeLimitExceededException exception (fully qualified name: org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) will be thrown when you call the parseRequest() method:

try {
List fileItemsList = servletFileUpload.parseRequest(request);
/* Process file items… */
}
catch (SizeLimitExceededException ex) {
/* The size of the HTTP request body exceeds the limit */
}

Iterating through File Items

Third, we will iterate through the file items and process each of them. The isFormField() method of the FileItem interface is used to determine whether a file item contains a simple name-value pair of a form field or an uploaded file:

Iterator it = fileItemsList.iterator();
while (it.hasNext()){
FileItem fileItem = (FileItem)it.next();
if (fileItem.isFormField()){
/* The file item contains a simple name-value pair of a form field */
}
else{
/* The file item contains an uploaded file */
}
}

(In the above Java code snippet, Iterator is an interface in the java.util package and FileItem is an interface in the org.apache.commons.fileupload package.)

Retrieving Values of Ordinary Form Fields with Java Servlet / JSP

If a file item contains a simple name-value pair of an ordinary form field, we can retrieve its name and value using the getFieldName() method and the getString() method respectively:

String name = fileItem.getFieldName();
String value = fileItem.getString();

For example, suppose there is a text field in an HTML/XHTML form:

Getting Information about the Uploaded File with Java Servlet / JSP

If a file item contains an uploaded file, we can use a number of methods to obtain some information about the uploaded file before we decide what to do with it:

/* Get the name attribute value of the element. */
String fieldName = fileItem.getFieldName();

/* Get the size of the uploaded file in bytes. */
long fileSize = fileItem.getSize();

String fileName = fileItem.getName();

String contentType = fileItem.getContentType();

Saving Uploaded Files in the File System with Java Servlet / JSP

In some situations, you just want to store the uploaded file in the file system without concerning what the uploaded file contains. The FileItem interface provides a method called write() that helps us perform this easily:

File saveTo = new File(”/upload_files/myFile.txt”);
fileItem.write(saveTo);

Processing Contents of Uploaded Files with Java Servlet / JSP

f you do not want to save the uploaded file directly but to process it, the get() and getInputStream() methods can help you. The get() method returns the uploaded file as an array of the byte data type:

byte[] fileData = fileItem.get();

However, if the uploaded file is large in size, you will not want to load the whole file into memory. The getInputStream() method can help you in this case. It returns the uploaded file as a stream:

InputStream fileStream = fileItem.getInputStream();

(InputStream is a class of the java.io package.)

 

Image resizing with PHP December 12, 2007

Filed under: Uncategorized — javatechie @ 11:23 am
Tags:

function imageResize($width, $height, $target) {

//takes the larger size of the width and height and applies the
formula accordingly…this is so this script will work
dynamically with any size image

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);

//returns the new sizes in html image tag format…this is so you
can plug this function inside an image tag and just get the

return “width=\”$width\” height=\”$height\””;

}

?>

The Function in Action

//get the image size of the picture and load it into an array
$mysock = getimagesize(”images/sock001.jpg”);

?>

<!—using a standard html image tag, where you would have the
width and height, insert your new imageResize() function with
the correct attributes –>

<img src=”images/sock001.jpg” >