Tuesday, March 10, 2009

Upload file using java

/**
*
*/
package com.jijo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
* @author jijo mathew
*
*/
public class Util {
public static void main(String args[]) {
try{
String outPath = "C:/images/jijo.jpg";
String inputPath = "C:/jijo.jpg";
File f=new File(inputPath);
InputStream inputStream= new FileInputStream(f);
String uploadPath = "C:/images/123.jpg";
uploadFile(uploadPath,inputStream); // for file upload
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void uploadFile(String filename,InputStream stream)
{
try {
OutputStream bos = new FileOutputStream(filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
}catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}catch (Exception ioe) {
ioe.printStackTrace();
}
}

}

No comments: