CodeCharge Studio
search Register Login  

Visual 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
santhosh
Posted: 09/27/2004, 8:03 AM

Quote Roger:
Hi,

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

R.
woranl

Posts: 1
Posted: 10/16/2004, 6:31 PM

Hi all, I really need your help. Please help me with this.
I'm using Jupload applet (the GPL version: http://sourceforge.net/projects/jupload/)

If I upload a file say "ABC.gif ", the applet will save my file into something call writeOut.bin and not the ABC.gif I wanted.

After uploading, the applet will send a parameter to writeOut.jsp like this

<param name="postURL" value="http://localhost:8080/jupload/wjhk_jupload/wwwroot/pages/upload.jsp?URLParam=URL+Parameter+Value"/>

The following is writeOut.jsp

<%@ page language="java" import="java.io.*, java.sql.*, java.util.*" %>
<%
// This JSP will save the request Input Steam into a file.
String fileOut = "C:/WINDOWS/Temp/writeOut.bin";
try{
ServletInputStream in = request.getInputStream();
byte[] line = new byte[1024];
int bytes = 0;

FileOutputStream fileOutS = new FileOutputStream(fileOut);

while(0 <(bytes = in.read(line))){
fileOutS.write(line,0, bytes);
}

fileOutS.close();
fileOutS = null;
out.println("SUCCESSFUL : Upload Stream Saved to \"" + fileOut + "\".");
}catch(Exception e){
out.println("ERROR : Exception \"" + e.getMessage() + "\" Occured.");
}
%>



Because writeOut.jsp couldn't save my file correctly, I then used the code from Ramesh Kumar Swarnkar



<!-- 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);

}
%>


Now, I'm able to upload my file correctly. But the problem is I can only upload one file. When I run my Jupload applet and select say 2 files to upload, upload.jsp only save the first file and ignoring the second one.

Any idea how to fix this?

or if you have another solution instead of the above mentioned upload.jsp, please tell me.
View profile  Send private message
Inder Jeet
Posted: 10/20/2004, 4:55 AM

Hi, I have to save a file from client browser to server database. I have tried with the code given here by mr. Mahesh, but it is giving me SQL exception while inserting into a table having bolb type of column. The Exception is "invalid hex number". Can some body help me?
Chris Neo
Posted: 11/12/2004, 2:10 AM

Hi,
I need the JSP/Servlet code to upload some images ,retrive them and to display in a page or pages according to the parameter passed from the previous page(image name has to be compared with the parameter passed and displayed)
shamim
Posted: 11/18/2004, 6:10 AM

I have one problem, I would like to pass some more paramater in hidden fields along with uploading image. I want to get those paramets in uploading JSP file, but now I am not able to do so can anyone suggets me hoe to declare the <form enctype?> or any other technique

please.

Thanks in advance.
Adarsh
Posted: 11/21/2004, 3:12 AM

Could any please help me in this case !

1) I need to upload a JAVA file using (JSP or HTML)
2) Execute it (might be with the help of servlets)
3) show the results on the webpage. (HTML or JSP)

Idea is as below:

User visits the webpage, uploads his Java file, executes it by pressing the execute button on the webpage. Results or error is show in the webpage.

Thanks everyone,
Adarsh
adarsh

Posts: 1
Posted: 11/21/2004, 3:22 AM

I need help on this , please anyone can help
1) uploading a java file using a webpage ( html or jsp)
2) executing the java program by clicking a button
3) displaying the results on the webpage

Notion:

User visits the webpage , uploads his/her java file , press the execute button on the webpage, results are displayed in the same/other webpages.

any links or any help would be appreiciate,
Thank,
Adarsh
View profile  Send private message
Abha
Posted: 11/24/2004, 12:52 AM

Quote Roger:
Hi,

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

R.
peterr


Posts: 5971
Posted: 11/24/2004, 12:55 AM

Yes, you can create a record form and click on the "File Upload" component to insert it into the record form. You will then have your own example of the file upload.
Please refer to CodeCharge Studio documentation for more details: http://docs.codecharge.com/studio/html/Components/Objec...d/Overview.html
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
Raheel Rehman
Posted: 12/06/2004, 9:38 AM

Hello Guys,

I have tried this example and successfully load the file on the server.

-----------------Form.html-------------------------------------------

<form action="upload.jsp" method="POST" enctype="multipart/form-data">
<!-- enctype="multipart/form-data" -->
<input type="file" name="theFile"><br>

<input type="submit">
</form>

------------------------------ upload.jsp --------------------------------------------
<!-- 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);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());

//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;
saveFile = "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\ROOT\\Extrusions\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);


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

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

}
%>

As you can see the path in Extrusions this is the path where I can save the files.
It will work, I am sure please try as it is.

Raheel Rehman
Murthy
Posted: 12/07/2004, 12:27 AM

Hi Inder did u get the solution??
coz, i searching for the same
if u have the sol.
plz let me know and help me in this regard.

Thanks and Regards

Moorthi

Quote Inder Jeet:
Hi, I have to save a file from client browser to server database. I have tried with the code given here by mr. Mahesh, but it is giving me SQL exception while inserting into a table having bolb type of column. The Exception is "invalid hex number". Can some body help me?
SmallByte
Posted: 12/09/2004, 3:29 PM

Thanks "Raheel Rehman" This was teh best solutions of all. NO hasel of jar files. Thanks a lot.
One more small request - Can you tell me how to store multiple files.



Quote Raheel Rehman:
Hello Guys,

I have tried this example and successfully load the file on the server.

-----------------Form.html-------------------------------------------

<form action="upload.jsp" method="POST" enctype="multipart/form-data">
<!-- enctype="multipart/form-data" -->
<input type="file" name="theFile"><br>

<input type="submit">
</form>

------------------------------ upload.jsp --------------------------------------------
<!-- 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);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());

//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;
saveFile = "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\ROOT\\Extrusions\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);


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

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

}
%>

As you can see the path in Extrusions this is the path where I can save the files.
It will work, I am sure please try as it is.

Raheel Rehman
speedy_tortoise
Posted: 12/12/2004, 6:05 PM

Quote adarsh:
I need help on this , please anyone can help
1) uploading a java file using a webpage ( html or jsp)
2) executing the java program by clicking a button
3) displaying the results on the webpage

Notion:

User visits the webpage , uploads his/her java file , press the execute button on the webpage, results are displayed in the same/other webpages.

any links or any help would be appreiciate,
Thank,
Adarsh


any help on this?? cos i'm having the same problem here... thanks alot...
peterr


Posts: 5971
Posted: 12/12/2004, 8:34 PM

You can create a record form in CCS and click on the "File Upload" component to insert it into the record form. You will then have your own example of the file upload.
Please refer to CodeCharge Studio documentation for more details: http://docs.codecharge.com/studio/html/Components/Objec...d/Overview.html
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
Tom
Posted: 12/14/2004, 11:53 AM

I think we need to modify this code a little bit because I got the following error during testing with JDeveloper:

500 Internal Server Error
java.lang.IndexOutOfBoundsException: The supplied offset + length did not fit into the supplied byte[] (offset + length = 8192 + 14281 = 22473 vs byte[].length = 14281)

I have modified the following line of code:

byteRead = in.read(dataBytes, totalBytesRead,
formDataLength - totalBytesRead);

It is reasonable if the length = total length - whatever you have read.

Tom




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);

}
%>

Ramakrishna
Posted: 12/16/2004, 1:10 AM

Hi I ramakkrishna,require code to upload file using servlet/jsp.Please let me have
farid
Posted: 12/17/2004, 6:46 PM

Hi
While running the suggested code , posted by Ramesh Kumar Swarnkar , It runs fine in windows , but in Linux , I get the following exception error :

Exception :java.lang.IndexOutOfBoundsException

The exception comes from this line :
fileOut.write(dataBytes, startPos, (endPos - startPos));

But if I change this line with this :
fileOut.write(dataBytes);

the exception disappears, but the file doesn't get created .

I really appreciate any suggestion




Bindu
Posted: 12/20/2004, 12:03 AM

Hello friends,

I have one problem with fileuploading. The problem is i am not able to transfer more than 6.5Mb of data.
How can i do this using ftp. or any other solutions????????
Btech
Posted: 12/21/2004, 8:22 AM

Ramesh Kumar Swarnkar


After using the code to upload a file where is the file fileoutputstream saved.
Btech
Posted: 12/21/2004, 8:23 AM

Ramesh Kumar Swarnkar


After using the code to upload a file where is the file fileoutputstream saved.
Accela
Posted: 01/04/2005, 6:08 AM

Quote Btech:
Ramesh Kumar Swarnkar


After using the code to upload a file where is the file fileoutputstream saved.

I supposed in the working directory, but I couldn't find mine either.
Accela
Posted: 01/04/2005, 6:08 AM

Quote Btech:
Ramesh Kumar Swarnkar


After using the code to upload a file where is the file fileoutputstream saved.

I supposed in the working directory, but I couldn't find mine either.
Hau
Posted: 01/13/2005, 1:33 AM

It's saved in the c:windows/system32

Now anyone know how to save in other directory instead of c:windows/system32

I mean which part of the code need to be changed ?

Thx
Quote Accela:
Quote Btech:
Ramesh Kumar Swarnkar


After using the code to upload a file where is the file fileoutputstream saved.

I supposed in the working directory, but I couldn't find mine either.
hasan
Posted: 01/14/2005, 8:49 AM

using ramesh kumar swarnkar's code, the uploaded file is saved to the bin directory of the tomcat home directory. e.g for me: it's C:\tomcat\bin

now i am trying to modify raheel rahman's code to see if i can control where this uploaded file goes.
manoj
Posted: 01/21/2005, 2:28 AM

how we cut the part of the file extension or name from the file.
ilango
Posted: 01/27/2005, 10:45 PM

Hai friend,

I am ilango,I want to share some doubts with u.

I captured one image from logitech web cam using java media framework and default store in D Drive(Eg : D:/webcam.png).

Query:
Now i want to insert image file in my server database.
Database Name : photto_test.
Field name : photto.
Data type : image.

This is Program.

<%@ page import="java.sql.*,java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest,com.microsoft.jdbc.sqlserver.SQLServerDriver;"%>
<HEAD>
<meta http-equiv="Content-Type" content="multipart/form-data; charset=iso-8859-1">
</HEAD>

<%
String filename="d:\\webcam.png";
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://localhost:8080;databasename=master","","");
/*******HERE IF I NEED TO PUT MY SERVER IP INSTEAD OF LOCALHOST********/
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");
%>

WHEN I RUN TOMCAT SERVER AND THIS JSP FILE THE OUTPUT

"The Image is Added into Database" But tomcat server error " java.io.IOException: Posted content type isn't multipart/form-data"

But image is not inserted into database.

I am correctly set and import all packages.

Please its very urgent to me.Please help

hola
Posted: 01/28/2005, 3:58 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);

}
%>

Deepak Singh
Posted: 02/01/2005, 1:46 AM

The code for inserting image is working:-). but how will i fetch the image from database and show it in correct format.
Please help me !
Thanks in advance
Ashwin Pai
Posted: 02/01/2005, 3:49 AM

Is there any other technique for file upload other than the multipart form ?
phyler
Posted: 02/04/2005, 6:24 AM

hi evry1!

for the "fileUpload.jsp"
i just hav a couple of questions..sorry, i didn't get the code well...
on what line is the connection to the server set? where exactly is the file stored in the server?where is it defined? pls help me... thnks!
First Prev Page 2 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.

MS Access to Web

Convert MS Access to Web.
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.