cancel
Showing results for 
Search instead for 
Did you mean: 

Files Upload

Former Member
0 Kudos

Hello, i have EAR + DataBase, how can i upload images into the database within java?

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member187444
Participant
0 Kudos

hi?

How did you use this code. it doesnt work my j2ee engine. I found problem in.read statement. because if my read parameter above integer.maxvalue(32768) it gives null exception.

Former Member
0 Kudos

<!-- upload.jsp -->

<%

System.out.println("Start...");

String contentType = request.getContentType();

System.out.println("Content type is :: " + contentType);

if ((contentType != null)

&& (contentType.indexOf("multipart/form-data") >= 0))

{

DataInputStream in =

new DataInputStream(request.getInputStream());

int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

while (totalBytesRead < formDataLength)

{

byteRead =

in.read(dataBytes, totalBytesRead, formDataLength);

totalBytesRead += byteRead;

}

out.println("<BR><BR>");

out.println("<BR><BR>");

//out.println(dataBytes.toString());

out.println("<BR><BR>");

out.println("<BR><BR>");

String file = new String(dataBytes);

String saveFile =

file.substring(file.indexOf("filename=\"") + 10);

saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

saveFile =

saveFile.substring(

saveFile.lastIndexOf("
") + 1,

saveFile.indexOf("\""));

//out.println(saveFile);

out.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");

String boundary =

contentType.substring(lastIndex + 1, contentType.length());

//out.println(boundary);

out.println("<BR><BR>");

out.println("<BR><BR>");

int pos;

pos = file.indexOf("filename=\"");

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

int boundaryLocation = file.indexOf(boundary, pos) - 4;

int startPos = ((file.substring(0, pos)).getBytes()).length;

int endPos =

((file.substring(0, boundaryLocation)).getBytes()).length;

String data = file.substring(startPos, endPos);

out.println(data);

out.println("<BR><BR>");

out.println("<BR><BR>");

FileOutputStream fileOut = new FileOutputStream(saveFile);

//fileOut.write(dataBytes);

fileOut.write(data.getBytes());

fileOut.flush();

fileOut.close();

out.println("File saved as " + saveFile);

}

%>

Former Member
0 Kudos

Hi,

If you are looking for a way to upload File, I would strongly recomment this frame work.

Its easy and good. Add the necessary jars and try it.

Let me know, how it goes .

<a href="http://jakarta.apache.org/commons/fileupload/">Apche Fileupload</a>

regards

Vivek Nidhi

Former Member
0 Kudos

I mean: User with Admin rights(UME ROLES), uploads images into j2ee application from servlet