Monday, May 21, 2007

Convert embedded images into base64 in SVG using Java

Its a nice feature that you can include images in SVG. But the embedded images still refer to an external file. So while the svg is downloaded all the relevant image files have to be downloaded as well. To avoid this you can convert your images into Base64 before embedding. All the referred images will remain in the same SVG file, SVG will not refer external image files. The functions mentioned use the Base64 utility from here : Pass the SVG file as a string into the method fillBase64(). This will return an SVG string with your image files Base64 embedded. What I have done here is a small text parsing to find image tag and include the corresponding Base64 code. Enjoy!

public String fillBase64(String svg) throws Exception
String newSvg = "";


int imageStartPos = svg.indexOf("<image",0);
int imageEndPos = 0;
int imageUrlPos = 0;
int copyStartPos = 0;
int fileNameStartPos = 0;
int fileNameEndPos = 0;

while(imageStartPos != -1)
imageEndPos = svg.indexOf("/>",imageStartPos);
imageUrlPos = svg.indexOf("xlink:href=",imageStartPos);

if((imageUrlPos > imageStartPos)||(imageUrlPos < imageEndPos))
fileNameStartPos = imageUrlPos+12 ;
fileNameEndPos = svg.indexOf("\"",fileNameStartPos);
String imageFilePath = svg.substring(fileNameStartPos, fileNameEndPos);

String base64String = getBase64(imageFilePath);

new File(imageFilePath).delete();

newSvg += svg.substring(copyStartPos, fileNameStartPos);
newSvg += "data:image/jpeg;base64,"+base64String;

copyStartPos = fileNameEndPos;
imageStartPos = svg.indexOf("<image",imageEndPos+2);

newSvg += svg.substring(copyStartPos);
catch(Exception e)
System.out.println("Exception in fillBase64() : ");e.printStackTrace();

return newSvg;

private String getBase64(String filePath) throws Exception
String h = "";
System.out.println("PRINTING THE FILEPATH \""+filePath+"\"");
File f = new File(filePath);

byte[] b = new byte[(int)f.length()];
System.out.println("FILELENGTH : "+(int)f.length());
FileInputStream fis = new FileInputStream(f);;
h = Base64.encodeBytes(b);
catch(Exception e)
System.out.println("Exception in getBase64() : ");e.printStackTrace();

return h;

Tuesday, May 15, 2007

runtime file upload using java



import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

public class HttpMultiPartFileUpload {
private static String url =

public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
MultipartPostMethod mPost = new MultipartPostMethod(url);

// Send any XML file as the body of the POST request
File f1 = new File("C:\\Documents and Settings\\aruljose\\Desktop\\downloaded.jpg");
//File f2 = new File("academy.xml");
//File f3 = new File("academyRules.xml");

System.out.println("File1 Length = " + f1.length());
//System.out.println("File2 Length = " + f2.length());
//System.out.println("File3 Length = " + f3.length());

mPost.addParameter(f1.getName(), f1);
//mPost.addParameter(f2.getName(), f2);
//mPost.addParameter(f3.getName(), f3);

int statusCode1 = client.executeMethod(mPost);

System.out.println("statusLine>>>" + mPost.getStatusLine());

SERVER (ProcessFileUpload.jsp) :

<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import=""%>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Process File Upload</title>
System.out.println("Content Type ="+request.getContentType());

DiskFileUpload fu = new DiskFileUpload();
// If file size exceeds, a FileUploadException will be thrown

List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();

while(itr.hasNext()) {
FileItem fi = (FileItem);

//Check if not form field so as to only handle the file inputs
//else condition handles the submit button input
if(!fi.isFormField()) {
System.out.println("\nNAME: "+fi.getName());
System.out.println("SIZE: "+fi.getSize());
File fNew= new File("C:\\Documents and Settings\\aruljose\\Desktop\\downloaded1.jpg");

else {
System.out.println("Field ="+fi.getFieldName());
Upload Successful!!

Monday, May 14, 2007

read image from url, write to your disk using java

URL url = new URL("http://host/urlseg1/urlseg2");//image url
BufferedImage bi =;
ImageIO.write(bi,"jpg",new File("your file path - c:\\folder1\folder2\file.jpg"));

Monday, May 7, 2007

read and write image file (or other files) from one location to another

import javax.imageio.ImageIO;
import java.awt.image.*;

public class ReadImage
public static void main(String args[]) throws Exception
File f = new File("location1\\file1.jpg");
File f1 = new File("location2\\file2.jpg");

FileInputStream fis = new FileInputStream(f);
byte b[] = new byte[9999999];
int len =;

FileOutputStream fos = new FileOutputStream(f1);