CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Java

 Java/JSP file upload

Print topic Send  topic

Author Message
njava
Posted: 04/13/2005, 2:14 AM

Quote Ramesh Kumar Swarnkar:
Well Friend, I worked on this code successfull ; I hope it will solve your problem too :)

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
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;
}

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.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

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

}
%>

Quote :
how can i retrive value of other html tag hidden field ,textfield,checkbox etc they are in the same form
rajkumar
Posted: 04/14/2005, 3:12 AM

Quote Roger:
Quote rajkumar:
Hi,

Are there any java/JSP examples of the file upload?

R.

ajay
Posted: 04/15/2005, 5:24 AM

hi I am uploading image in one of my folder at my server . it is working fine but my image is copied to proper folder , it get copied to /BIN folder but i need it to /data/user folder from in my servar . lpease reply me asap
Stanislav Shpits
Posted: 04/15/2005, 8:56 AM

;-)
Here you go!!!
I have done this for 5 images to be uploaded, but it could be +++


<%@ page import="
java.io.*,
java.util.*,
java.text.*,
java.io.*,
java.rmi.RemoteException,
java.rmi.Remote,
javax.naming.*,
javax.ejb.*,
javax.servlet.*,
javax.servlet.http.*,
javax.rmi.PortableRemoteObject,
com.oreilly.servlet.multipart.*,
com.nationstreet.ejb.orderManager.*"
%>
<%!
boolean UploadImagesActivity(byte[] dataArray)
{
Connection con = null;
PreparedStatement ps = null;
String sql = "INSERT INTO hdusa_damagedgoods(img_file) values(?)";

try
{
con = getConnection();
ps = con.prepareStatement(sql);
ps.setBytes(1, dataArray);
ps.executeUpdate();
}
catch (SQLException e)
{
System.out.println("SQL: " + sql);
e.printStackTrace();
return false;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
finally
{
try
{
if (ps != null)
ps.close();
}
catch (Exception e) {}
try
{
if (!con.isClosed())
con.close();
}
catch (Exception e) {}
ps = null;
con = null;
}
return true;
}

%>
<%

int maxFileSize = 20 * 1024 * 1024; // 20MB max
MultipartParser parserFormData = new MultipartParser(request, maxFileSize);
Part p = null;
long numBytes = 0;
byte[] fileDataArray1 = null;
byte[] fileDataArray2 = null;
byte[] fileDataArray3 = null;
byte[] fileDataArray4 = null;
byte[] fileDataArray5 = null;

try
{

while((p = parserFormData.readNextPart()) != null)
{

if(p.isFile())
{
FilePart fp = (FilePart)p;
InputStream fip = fp.getInputStream();
ByteArrayOutputStream bias = new ByteArrayOutputStream();

if(fp.getFileName() != null)
{
int intSize = 0;
while((intSize = fip.read()) != -1)
{
bias.write((byte)intSize);
}

if (p.getName().equals("image1") && !fp.getFileName().equals(""))
{
fileDataArray1 = bias.toByteArray();
}
if (p.getName().equals("image2") && !fp.getFileName().equals(""))
{
fileDataArray2 = bias.toByteArray();
}
if (p.getName().equals("image3") && !fp.getFileName().equals(""))
{
fileDataArray3 = bias.toByteArray();
}
if (p.getName().equals("image4") && !fp.getFileName().equals(""))
{
fileDataArray4 = bias.toByteArray();
}
if (p.getName().equals("image5") && !fp.getFileName().equals(""))
{
fileDataArray5 = bias.toByteArray();
}
}
}
}

if(fileDataArray1 != null && fileDataArray1.length > 0)
{
UploadImagesActivity(fileDataArray1);
}

if(fileDataArray2 != null && fileDataArray2.length > 0)
{
UploadImagesActivity(fileDataArray2);
}

if(fileDataArray3 != null && fileDataArray3.length > 0)
{
UploadImagesActivity(fileDataArray3);
}

if(fileDataArray4 != null && fileDataArray4.length > 0)
{
UploadImagesActivity(fileDataArray4);
}

if(fileDataArray5 != null && fileDataArray5.length > 0)
{
UploadImagesActivity(fileDataArray5);
}

}
catch (Exception e)
{
e.printStackTrace();
}
%>
;-)
aisha
Posted: 04/18/2005, 12:05 PM

I want to upload file to oracle database . there is any body have a code to solve my problem.
ramesh
Posted: 04/23/2005, 4:56 AM

can u provide me without using packages storing an image into database sqlserver
geo
Posted: 04/27/2005, 11:06 PM


How do you display the image in your database? I mean, I want to display the image i stored in my database to my jsp.


Quote mahesh:
Hai to Every one I have a solution to JSP Insert image into Database

<%-- This jsp Page is Uploding The Image into Database using MultipartRequest class
FileSystem concepts and ByteArray.
--%>

<%@ page import="java.sql.*,java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest,com.microsoft.jdbc.sqlserver.SQLServerDriver;"%>
<%

/* The Following Code is Used To Insert An Image Into Database */

String filename="";
try
{
//Download com.oreilly package

MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);
Enumeration files=multi.getFileNames();
File f=null;
while(files.hasMoreElements())
{
String name=(String)files.nextElement();
filename=multi.getFilesystemName(name);
String type=multi.getContentType(name);
f=multi.getFile(name);
System.out.println("The File is "+f);
}
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver;databasename="xxx","username","password");
Statement stmt = con.createStatement();
InputStream is = new FileInputStream(f);
byte b[]=new byte[is.available()];
is.read(b);
String sql = "INSERT into photo_test (\"Photo\") values('" + b + "')";
stmt.execute(sql);
stmt.close();
}catch(Exception e)
{
System.out.println(e);
}
out.println("The Image is Added into Database");

This code is perfectly working , I could store an image into database using this program but i am unable to retrieve the page. The image is getting decoded but when i am trying to send the content to a file and open it, it is giving me the name fo the file but the picture is not getting opened. So any one can help me.
Thanks advans
sandhya
Posted: 05/06/2005, 12:04 AM

friends,
i am trying for uploading files in java
please send me sample examples in servlets and jsp

sandhya
Praveen Kumar
Posted: 05/11/2005, 7:40 AM

how to upload image file from client to server using jsp
Parag
Posted: 05/14/2005, 12:06 AM

Hi,

In the code given by Ramesh I am getiing content type as null even though I set it in the JSP file which is calling this code.

I have tow JSP files one contain the UI part i.e browse and upload button. On click of upload button I am calling the other JSP which is contains the code given by Ramesh. This code is to be run on client side or server side???

Can ne way tell me how to set the content type?

Its ver urgent pls reply soon.

Parag
chanaka
Posted: 05/20/2005, 1:22 AM

Quote Roger:
Hi,

Are there any java/JSP examples of the file upload?

R.
peterr


Posts: 5971
Posted: 05/20/2005, 9:23 AM

Yes, at http://examples.codecharge.com/CCSExamplePack2/FileUpload/FileUpload.php
You can create it with CodeCharge Studio, of course.
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
Szymon Splawski
Posted: 05/20/2005, 9:35 AM

Go to topic: "I have only correct solution..." - there is an answer.
Arun
Posted: 05/24/2005, 9:37 PM

Hi,
I need to upload rows of data each row having file to ne uplaoded and few textboxes of additional Information.
Has nybody handled such form in jsp.
Prompt reply will be of great help.


Arun

stan
Posted: 06/03/2005, 2:04 AM

Hai,

I need a sample code using jsp/java to store the image into serverside (like image folder)
Boms
Posted: 06/03/2005, 6:56 AM

Quote Ramesh Kumar Swarnkar:
Well Friend, I worked on this code successfull ; I hope it will solve your problem too :)

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
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;
}

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.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

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

}
%>

8-) 8-)
Gopi
Posted: 06/04/2005, 12:30 AM

Hi,
can you please mail me the entire source code. I want go through it.
Thanks,
Gopi.
Quote Ramesh Kumar Swarnkar:
Well Friend, I worked on this code successfull ; I hope it will solve your problem too :)

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
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;
}

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.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

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

}
%>

Amit
Posted: 06/06/2005, 2:18 AM

This is one of the best reply.
One suggestion can we have a filter on upload..means if i want to upload only .doc and .xls files. plz help out
Tejaswini
Posted: 06/12/2005, 10:16 PM

Quote Kate:
Hi
I am trying to upload file from cleint machine to the database in the server side using Java.I have to give it a name and ID before saving it to the database. I am also using Tomcat.I am a novice in this field.
Any suggestion,direction or sample would be a great help and would be greatly appreciated.
Thanks
Bharadwaj
Posted: 06/17/2005, 6:51 PM

Quote phani:
Quote Thuanvd:
I want to have a code for upload image with jsp/servlet



<%@ page import="java.sql.*,java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest,com.microsoft.jdbc.sqlserver.SQLServerDriver;"%>
<%

/* The Following Code is Used To Insert An Image Into Database

String filename="";
try
{
MultipartRequest multi= new MultipartRequest(request,"d:/phani",5*1024*1024);
Enumeration files=multi.getFileNames();
File f=null;
while(files.hasMoreElements())
{
String name=(String)files.nextElement();
filename=multi.getFilesystemName(name);
String type=multi.getContentType(name);
f=multi.getFile(name);
System.out.println("The File is "+f);
}
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://172.21.1.11:1740;databasename=pubs","sa","satest");
Statement stmt = con.createStatement();
System.out.println("2 "+f);
InputStream is = new FileInputStream(f);
System.out.println("4 "+is);
byte b[]=new byte[is.available()];
is.read(b);
String sql = "INSERT into photo_test (\"Photo\") values('" + b + "')";
System.out.println("sql is " +sql);
stmt.execute(sql);
stmt.close();

}catch(Exception e)
{
System.out.println(e);
}
out.println("The Image is Added into Database");

sqa
Posted: 06/18/2005, 7:31 AM

Quote mahesh:
String sql = "INSERT into photo_test (\"Photo\") values('" + b + "')";

i think this only inserted nothing but a string into image, so you can never open it... and the string is something like "java.io.FileInputStream@121cc40".
khaled
Posted: 06/20/2005, 6:56 AM

Dear Sir,

I tried your code. But I'm not sure to which directory does it save the file.
This directory should be on the server. Isn't it?
I tried adding a path but I still can't find the file. Can you help?

I'm really new to JAVA programming.

Regards,

Quote Ramesh Kumar Swarnkar:
Well Friend, I worked on this code successfull ; I hope it will solve your problem too :)

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
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;
}

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.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

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

}
%>

Borja
Posted: 06/27/2005, 3:14 AM

I was really happy when i found this topic, because I thought it could save all my problems, but I have the next problem on my server when I execute your code:

java.lang.IndexOutOfBoundsException: The supplied offset + length did not fit into the supplied byte[] (offset + length = 7619 + 21580 = 29199 vs byte[].length = 21580)


Do you have any solutions to this???

Thank you very much...

------------------------------------

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
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;
}

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.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

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

}
%>

----------------------------------
Rudra
Posted: 06/28/2005, 1:33 AM

Nice help website
Gautam
Posted: 06/30/2005, 3:02 AM

your help is nice..... very nice:-) :-D
Kalpana
Posted: 07/04/2005, 12:42 AM

hi!!!!!!!!!!


It was useful for me while coding !

With Regards!!
Kalpana
Jaya
Posted: 07/06/2005, 5:27 AM

Quote Roger:
Hi,

Are there any java/JSP examples of the file upload?

R.
ques
Posted: 07/12/2005, 12:52 AM

i have to upload in java a files with .txt extension from c:\doc1 to a server destination in c:\doc2. without using a form or an input
please help me
gaurav mishra
Posted: 07/12/2005, 1:29 AM

Quote Roger:
Curtis,

I've tried to get the basics working. Is there a quick howto or have you a sample I could look at.

Basically I'd like to upload an emp image into a database field using java/jsp.

Direction would be greatly appreciated

Regards

R.

waqasahmedch
Posted: 07/18/2005, 3:27 AM

It give me error in while loop.
Error Occured.java.lang.IndexOutOfBoundsException: The supplied offset + length did not fit into the supplied byte[] (offset + length = 8192 + 77535 = 85727 vs byte[].length = 77535)

Quote Ramesh Kumar Swarnkar:
Well Friend, I worked on this code successfull ; I hope it will solve your problem too :)

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
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;
}

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.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

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

}
%>

First Prev Page 4 of 5  Next Last


Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.