Thursday, July 30, 2009

Resolving heap space problem

Eclipse and memory settings

eclipse.ini is a configuration file that is located in the root of your eclipseinstallation which is used as the default arguments passed to eclipse..
--launcher.XXMaxPermSize
512M
-vmargs
-Xms1024M
-Xmx1536m

Environment varialbe
JAVA_OPT java -Xms2048m -Xmx2441m

Friday, July 24, 2009

How to upload a file in to the ftp server through java

package com.evolvus.documentmanagement.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;

/***
* This is an program demonstrating how to use the FTPClient class.
* This program connects to an FTP server and retrieves the specified
* file.
***/
public final class FTPUtil
{
public boolean insertIntoFTP(String server,String port,String username,String password,String remote,String local,String directoryName,byte[] data){
boolean error = false;
FTPClient ftp = null;
try
{
ftp = new FTPClient();
this.getFTPConnection(ftp, server,port, username, password,directoryName);
if(ftp!=null){
InputStream input;
input = new ByteArrayInputStream(data);
ftp.storeFile(remote, input);
}
}catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
}
return error;
}
public byte[] getDataFromFTP(String server,String port,String username,String password,String remote,String directoryName,String local){
byte[] data = null ;
boolean error = false;
FTPClient ftp = null;
try
{
ftp = new FTPClient();
this.getFTPConnection(ftp, server,port, username, password,directoryName);
if(ftp!=null){
ByteArrayOutputStream output;

output = new ByteArrayOutputStream();
ftp.retrieveFile(remote, output);
data=output.toByteArray() ;
output.close();
}
}
catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
return data;
}
}

public FTPClient getFTPConnection(FTPClient ftp,String server,String port,String username,String password,String directoryName){
boolean error = false;
try
{
int reply;
if(port!=null &&port.trim().length()>0)
ftp.connect(server,Integer.parseInt(port));
else
ftp.connect(server);
System.out.println("Connected to " + server + ".");

// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
System.err.println("FTP server refused connection.");
}
if (!ftp.login(username, password))
{
ftp.logout();
error = true;
}
if(!ftp.changeWorkingDirectory(directoryName)){
ftp.mkd(directoryName);
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
catch (IOException e)
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
}
return ftp;
}
public static void main(String[] args) {
FTPUtil util = new FTPUtil();
util.insertIntoFTP("192.168.1.1", "65300", "user1", "ftp*evolvus", "test123", "test", "test123", "test".getBytes());
}
}

Friday, July 10, 2009

Multiple file upload using Apache Struts



Uploading of file using struts with the FormFile is easy! But the requirement that I got was not as sweet as I thought. I had to upload multiple files and the user had the facility to add files before doing the upload. Thus I had to have a “Add File” button that will present the user with one more column to select a file using the browse button.

Having the facility of FormFile and the apache upload package, I thought the task would be easy but I had tough time due the dynamic part of this task. In the end I solved the problem. And here is the solution for you people as you should not reinvent the wheel. :)

Add button problem was solved by a simple java script that created an INPUT tag and inserted to the existing form. The code is given below:

function addFileElement() {
fCount++;
var fObject = document.getElementById('fileSection');
var text = 'File:';
var tag='+fCount+']" value="">';
var brk='
';

var o1 = document.createTextNode(text);
var o2 = document.createElement(tag);
var o3 = document.createElement(brk);

fObject.appendChild(o3);
fObject.appendChild(o1);
fObject.appendChild(o2);
fObject.appendChild(o3);

}

As usual the form bean of struts had a FormFile and an ArrayList variable that would hold the entire set of files. Now, I the form bean we tune the setter method of FormFile so that, each time the function is called the new file that is being set is added to the ArrayList. The code is given below:

public void setTestFile(int in,FormFile t) {

try {

this.testFile = t; // testFile is FormFile’s obj
setFormFiles(t); // add the file to array
index++;
}catch(Exception e) {
System.out.println("Exception in setTestFile!" + e);
}

}

And now the jsp code! It’s fairly simple and sweet. All you have to do is provide a strut form that has "browse" buttons, a "submit" button and a “add” button. The add button will run the above java script and you will get more options to upload file!