Debamalya Jha
|
Posted: 09/15/2005, 5:54 AM |
|
The programs given in Java Forum is not suitable for long .pdf
files.Can you give a code that will run for a long file.
|
|
|
JZ
|
Posted: 10/07/2005, 1:04 PM |
|
Try UploadBean. It works for any file and it allows to upload very large files with low memory : http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html
|
|
|
fun2oos
Posts: 4
|
Posted: 11/03/2005, 2:19 AM |
|
jsp file upload code (fileUpload.jsp):
(can be used to upload any type of file)
(modify it as u like so as to enable large file uploading etc.
and do tell me also the changes required. )
***u can uncomment the comments to see the intermediate results)
<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>
<%@ page import="java.io.FileWriter,java.io.IOException" %>
<%
String savePath = "", filepath = "", filename = "";
String contentType = "", fileData = "", strLocalFileName = "";
int startPos = 0, endPos = 0;
int BOF = 0, EOF = 0;
%>
<%!
//copy specified number of bytes from main data buffer to temp data buffer
void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)
{
for(int i=start;i<(start+len);i++)
{
toBytes[i - start] = fromBytes;
}
}
%>
<%
contentType = request.getContentType();
out.println("<br>Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
DataInputStream in1 = in;
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
out.println("<br>totalBytesRead : " + totalBytesRead + " : formDataLength = " + formDataLength);
//String file = new String(dataBytes);
//out.println("<br>File Contents:<br>////////////////////////////////////<br>" + file + "<br>////////////////////////////////<br>");
byte[] line = new byte[128];
if (totalBytesRead < 3)
{
return; //exit if file length is not sufficiently large
}
String boundary = "";
String s = "";
int count = 0;
int pos = 0;
//loop for extracting boundry of file
//could also be extracted from request.getContentType()
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("Content-Disposition: form-data; name=\""); //set the file name
if(pos != -1)
endPos = pos;
}while(pos == -1);
boundary = fileData.substring(startPos, endPos);
//loop for extracting filename
startPos = endPos;
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("filename=\"", startPos); //set the file name
if(pos != -1)
startPos = pos;
}while(pos == -1);
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("Content-Type: ", startPos);
if(pos != -1)
endPos = pos;
}while(pos == -1);
filename = fileData.substring(startPos + 10, endPos - 3); //to eliminate " from start & end
strLocalFileName = filename;
int index = filename.lastIndexOf("\\");
if(index != -1)
filename = filename.substring(index + 1);
else
filename = filename;
//loop for extracting ContentType
boolean blnNewlnFlag = false;
startPos = endPos; //added length of "Content-Type: "
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("\n", startPos);
if(pos != -1)
{
if(blnNewlnFlag == true)
endPos = pos;
else
{
blnNewlnFlag = true;
pos = -1;
}
}
}while(pos == -1);
contentType = fileData.substring(startPos + 14, endPos);
//loop for extracting actual file data (any type of file)
BOF = count + 1;
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf(boundary, startPos); //check for end of file data i.e boundry value
}while(pos == -1);
EOF = count - boundary.length();
//file data extracted
out.println("<br><br>0. Local File Name = " + strLocalFileName);
out.println("<br><br>1. filename = " + filename);
out.println("<br>2. contentType = " + contentType);
out.println("<br>3. startPos = " + BOF);
out.println("<br>4. endPos = " + EOF);
out.println("<br>5. boundary = " + boundary);
//create destination path & save file there
String appPath = application.getRealPath("/");
out.println("<br>appPath : " + appPath);
String destFolder = appPath + "images/banner/"; //change this as required
filename= destFolder + filename;
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.write(dataBytes, BOF, (EOF - BOF));
fileOut.flush();
fileOut.close();
out.println("<br>File saved as >> " + filename);
//file saved at destination
//out.println("<br>File data : <br><br>**************************<br>" + (new String(dataBytes,startPos, (endPos - startPos))) + "<br><br>**************************");
}
else
{
out.println("Error in uploading ");
}
%>
supporting HTML file for above JSP:
<form method="post" action="fileUpload.jsp" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="uploadfile" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
enjoy
:)
_________________
fun2oos |
|
|
fun2oos
Posts: 4
|
Posted: 11/03/2005, 2:22 AM |
|
jsp file upload code (fileUpload.jsp):
(can be used to upload any type of file)
(modify it as u like so as to enable large file uploading etc.
and do tell me also the changes required. )
***u can uncomment the comments to see the intermediate results)
<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>
<%@ page import="java.io.FileWriter,java.io.IOException" %>
<%
String savePath = "", filepath = "", filename = "";
String contentType = "", fileData = "", strLocalFileName = "";
int startPos = 0, endPos = 0;
int BOF = 0, EOF = 0;
%>
<%!
//copy specified number of bytes from main data buffer to temp data buffer
void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)
{
for(int i=start;i<(start+len);i++)
{
toBytes[i - start] = fromBytes;
}
}
%>
<%
contentType = request.getContentType();
out.println("<br>Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
DataInputStream in1 = in;
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
out.println("<br>totalBytesRead : " + totalBytesRead + " : formDataLength = " + formDataLength);
//String file = new String(dataBytes);
//out.println("<br>File Contents:<br>////////////////////////////////////<br>" + file + "<br>////////////////////////////////<br>");
byte[] line = new byte[128];
if (totalBytesRead < 3)
{
return; //exit if file length is not sufficiently large
}
String boundary = "";
String s = "";
int count = 0;
int pos = 0;
//loop for extracting boundry of file
//could also be extracted from request.getContentType()
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("Content-Disposition: form-data; name=\""); //set the file name
if(pos != -1)
endPos = pos;
}while(pos == -1);
boundary = fileData.substring(startPos, endPos);
//loop for extracting filename
startPos = endPos;
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("filename=\"", startPos); //set the file name
if(pos != -1)
startPos = pos;
}while(pos == -1);
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("Content-Type: ", startPos);
if(pos != -1)
endPos = pos;
}while(pos == -1);
filename = fileData.substring(startPos + 10, endPos - 3); //to eliminate " from start & end
strLocalFileName = filename;
int index = filename.lastIndexOf("\\");
if(index != -1)
filename = filename.substring(index + 1);
else
filename = filename;
//loop for extracting ContentType
boolean blnNewlnFlag = false;
startPos = endPos; //added length of "Content-Type: "
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("\n", startPos);
if(pos != -1)
{
if(blnNewlnFlag == true)
endPos = pos;
else
{
blnNewlnFlag = true;
pos = -1;
}
}
}while(pos == -1);
contentType = fileData.substring(startPos + 14, endPos);
//loop for extracting actual file data (any type of file)
BOF = count + 1;
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf(boundary, startPos); //check for end of file data i.e boundry value
}while(pos == -1);
EOF = count - boundary.length();
//file data extracted
out.println("<br><br>0. Local File Name = " + strLocalFileName);
out.println("<br><br>1. filename = " + filename);
out.println("<br>2. contentType = " + contentType);
out.println("<br>3. startPos = " + BOF);
out.println("<br>4. endPos = " + EOF);
out.println("<br>5. boundary = " + boundary);
//create destination path & save file there
String appPath = application.getRealPath("/");
out.println("<br>appPath : " + appPath);
String destFolder = appPath + "images/banner/"; //change this as required
filename= destFolder + filename;
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.write(dataBytes, BOF, (EOF - BOF));
fileOut.flush();
fileOut.close();
out.println("<br>File saved as >> " + filename);
//file saved at destination
//out.println("<br>File data : <br><br>**************************<br>" + (new String(dataBytes,startPos, (endPos - startPos))) + "<br><br>**************************");
}
else
{
out.println("Error in uploading ");
}
%>
supporting HTML file for above JSP:
<form method="post" action="fileUpload.jsp" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="uploadfile" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
enjoy
:)
_________________
fun2oos |
|
|
Mani
|
Posted: 11/10/2005, 3:12 AM |
|
hi,
when i am trying to run this code i get error
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 9 in the jsp file: /fileUpload.jsp
Generated servlet error:
D:\Tools\jboss-3.2.6\server\default\work\jboss.web\localhost\text\org\apache\jsp\fileUpload_jsp.java:21: illegal start of expression
toBytes[i - start] = []fromBytes;
^
1 error
Thanks
|
|
|
mani
|
Posted: 11/10/2005, 3:33 AM |
|
i must be sleeping when i typed the above error
This is error i am getting
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 9 in the jsp file: /fileUpload.jsp
Generated servlet error:
D:\Tools\jboss-3.2.6\server\default\work\jboss.web\localhost\text\org\apache\jsp\fileUpload_jsp.java:21: incompatible types
found : byte[]
required: byte
toBytes[i - start] = fromBytes;
^
1 error
|
|
|
Saurabh
|
Posted: 11/21/2005, 3:39 AM |
|
I am getting following error :illegal statrt of expression in
private static java.util.Vector _jspx.dependent()
public java.util.List java.util.List getDependents()
{}
Pls give me the reason why i am getting this error...
|
|
|
thinfile
Posts: 1
|
Posted: 11/23/2005, 10:47 PM |
|
You are seeing this error because fromBytes is an array while the toBytes[i - start] is a scalar. I think the other poster intended it to be:
toBytes[i - start] = fromBytes
I believe the original post is regarding handling an upload at the server using servlets. The most popular tool for this purpose is the jakarta commons upload handler. It's very easy to use.
--
[url=http://upload.thinfile.com/resume/]Thin Slice Upload[/url]
Upload your elephant.
|
|
|
Gomateswaran
|
Posted: 12/13/2005, 5:38 AM |
|
change the line to
toBytes[i - start] = fromBytes;
it will work
|
|
|
nice
|
Posted: 12/17/2005, 1:39 AM |
|
hi
pls help me...
we ve to compile c and cpp programs in server side eith given inputs which r uploaded in client side.... using JSP ..
|
|
|
Anup
|
Posted: 12/30/2005, 4:25 AM |
|
change the line to
toBytes[i - start] = fromBytes;
it is working i am using it now
|
|
|
A.R.Rajesh
|
Posted: 01/02/2006, 11:21 PM |
|
change the line to
**********************************
for(int i=start;i<(start+len);i++)
{
toBytes[i-start] = fromBytes;
}
|
|
|
kumar g
|
Posted: 01/03/2006, 11:11 PM |
|
Changed it still not working
toBytes[i - start] = fromBytes;
|
|
|
pfos.bmth
|
Posted: 01/10/2006, 4:32 PM |
|
Try this, it works for me.
toBytes[i - start] = fromBytes[ i ]; Theres a problem with typing [ i ] without the spaces on this site. I think its a formatting tag for italics. Thats why it doesnt appear in the original code, or any of the replies.
Hope that helps
|
|
|
joy
|
Posted: 01/12/2006, 5:18 AM |
|
Quote fun2oos:
jsp file upload code (fileUpload.jsp):
(can be used to upload any type of file)
(modify it as u like so as to enable large file uploading etc.
and do tell me also the changes required. )
***u can uncomment the comments to see the intermediate results)
<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>
<%@ page import="java.io.FileWriter,java.io.IOException" %>
<%
String savePath = "", filepath = "", filename = "";
String contentType = "", fileData = "", strLocalFileName = "";
int startPos = 0, endPos = 0;
int BOF = 0, EOF = 0;
%>
<%!
//copy specified number of bytes from main data buffer to temp data buffer
void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)
{
for(int i=start;i<(start+len);i++)
{
toBytes[i - start] = fromBytes;
}
}
%>
<%
contentType = request.getContentType();
out.println("<br>Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
DataInputStream in1 = in;
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
out.println("<br>totalBytesRead : " + totalBytesRead + " : formDataLength = " + formDataLength);
//String file = new String(dataBytes);
//out.println("<br>File Contents:<br>////////////////////////////////////<br>" + file + "<br>////////////////////////////////<br>");
byte[] line = new byte[128];
if (totalBytesRead < 3)
{
return; //exit if file length is not sufficiently large
}
String boundary = "";
String s = "";
int count = 0;
int pos = 0;
//loop for extracting boundry of file
//could also be extracted from request.getContentType()
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("Content-Disposition: form-data; name=\""); //set the file name
if(pos != -1)
endPos = pos;
}while(pos == -1);
boundary = fileData.substring(startPos, endPos);
//loop for extracting filename
startPos = endPos;
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("filename=\"", startPos); //set the file name
if(pos != -1)
startPos = pos;
}while(pos == -1);
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("Content-Type: ", startPos);
if(pos != -1)
endPos = pos;
}while(pos == -1);
filename = fileData.substring(startPos + 10, endPos - 3); //to eliminate " from start & end
strLocalFileName = filename;
int index = filename.lastIndexOf("\\");
if(index != -1)
filename = filename.substring(index + 1);
else
filename = filename;
//loop for extracting ContentType
boolean blnNewlnFlag = false;
startPos = endPos; //added length of "Content-Type: "
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf("\n", startPos);
if(pos != -1)
{
if(blnNewlnFlag == true)
endPos = pos;
else
{
blnNewlnFlag = true;
pos = -1;
}
}
}while(pos == -1);
contentType = fileData.substring(startPos + 14, endPos);
//loop for extracting actual file data (any type of file)
BOF = count + 1;
do
{
copyByte(dataBytes, line, count ,1); //read 1 byte at a time
count+=1;
s = new String(line, 0, 1);
fileData = fileData + s;
pos = fileData.indexOf(boundary, startPos); //check for end of file data i.e boundry value
}while(pos == -1);
EOF = count - boundary.length();
//file data extracted
out.println("<br><br>0. Local File Name = " + strLocalFileName);
out.println("<br><br>1. filename = " + filename);
out.println("<br>2. contentType = " + contentType);
out.println("<br>3. startPos = " + BOF);
out.println("<br>4. endPos = " + EOF);
out.println("<br>5. boundary = " + boundary);
//create destination path & save file there
String appPath = application.getRealPath("/");
out.println("<br>appPath : " + appPath);
String destFolder = appPath + "images/banner/"; //change this as required
filename= destFolder + filename;
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.write(dataBytes, BOF, (EOF - BOF));
fileOut.flush();
fileOut.close();
out.println("<br>File saved as >> " + filename);
//file saved at destination
//out.println("<br>File data : <br><br>**************************<br>" + (new String(dataBytes,startPos, (endPos - startPos))) + "<br><br>**************************");
}
else
{
out.println("Error in uploading ");
}
%>
supporting HTML file for above JSP:
<form method="post" action="fileUpload.jsp" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="uploadfile" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
enjoy
:)
Quote :How to make large file uploads...can u let me know ...urgent?????????????
|
|
|
jarol
|
Posted: 01/13/2006, 10:21 AM |
|
can i have a code to upload files from remote to myserver
thanks
|
|
|
pfos.bmth
|
Posted: 01/14/2006, 4:24 PM |
|
Jakarta Commons FileUpload allows you to process parameters posted in the form along with files. It can process many files as well. Heres an example:
Process_FileUpload.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<html>
<head>
<title>Commons-FileUpload-1.1 Report Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%!
//method to return file extension
String getFileExt(String xPath){
//Find extension
int dotindex = 0; //extension character position
dotindex = xPath.lastIndexOf('.');
if (dotindex == -1){ // no extension
return "";
}
int slashindex = 0; //seperator character position
slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));
if (slashindex == -1){ // no seperator characters in string
return xPath.substring(dotindex);
}
if (dotindex < slashindex){ //check last "." character is not before last seperator
return "";
}
return xPath.substring(dotindex);
}
%>
<%
// Check that we have a file upload request
boolean isMultipart = FileUpload.isMultipartContent(request);
out.println("isMultipart="+isMultipart+"<br>");
//Create variables for path, filename and extension
String newFilePath = application.getRealPath("/") + "LOCATIONOFFILE";
String newFileName ="";
String FileExt = "";
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
//Form fields
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if (name.equals("newFileName")) {
newFileName = value;
}
out.println("Form Field Name= " + name + "<br>");
out.println("Form Field Value= " + value + "<br><br>");
}
else {
String fieldName = item.getFieldName();
String fileName = item.getName();
FileExt = getFileExt(fileName);
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println("fieldName= " + fieldName + "<br>");
out.println("fileName= " + fileName + "<br>");
out.println("FileExt= " + FileExt + "<br>");
out.println("contentType= " + contentType + "<br>");
out.println("isInMemory= " + isInMemory + "<br>");
out.println("sizeInBytes= " + sizeInBytes + "<br>");
if (fileName.equals("") || sizeInBytes==0){
out.println("Not a valid file.<br>No upload attempted.<br><br>");
} else {
File uploadedFile = new File(newFilePath+"/", newFileName+FileExt);
try{
item.write(uploadedFile);
out.println(fileName+" was successfully uploaded as "+ newFileName+FileExt +".<br><br>");
}
catch (java.lang.Exception e) {
out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br><br>");
}
}
}
}
%>
</body>
</html>
The above code will only work if the new filename is processed before the file.
Heres some sample html that will upload 3 files of any kind, using values posted in hidden fields to pass new filenames:
<body>
<form method="post" action="Process_FileUpload.jsp" name="uploadform" enctype="multipart/form-data">
<table >
<tr>
<td>Select up to 3 files to upload</td>
</tr>
<tr>
<td>
<input type="hidden" name="newFileName" value="newUploadedFileName1">
<input type="file" name="uploadfile" >
</td>
</tr>
<tr>
<td>
<input type="hidden" name="newFileName" value="newUploadedFileName2">
<input type="file" name="uploadfile" >
</td>
</tr>
<tr>
<td>
<input type="hidden" name="newFileName" value="newUploadedFileName3">
<input type="file" name="uploadfile" >
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
|
|
|
jarol
|
Posted: 01/15/2006, 6:41 AM |
|
how can i get the following classes?
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
|
|
|
jarol
|
Posted: 01/16/2006, 6:04 AM |
|
org.apache.jasper.JasperException: Unable to compile class for JSP
this error has been occured when i compile it.Do you have a solution for this
thanks
|
|
|
pfos.bmth
|
Posted: 01/16/2006, 3:36 PM |
|
Quote jarol:
how can i get the following classes?
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
you need to download jakarta commons FileUpload package, and put it into your WEB-INF folder.
http://jakarta.apache.org/commons/fileupload/
You'll also need commons IO package.
|
|
|
tmaker
|
Posted: 01/18/2006, 1:44 AM |
|
Thanks a lot 'pfos.bmth', your code certainly works great. Thanks for your time.
|
|
|
dingo
Posts: 3
|
Posted: 01/18/2006, 2:48 PM |
|
It works great (for me) until it finishes the Process_FileUpload - and then every time no matter which file size, I get an "Errors prevented the file upload" every time..
I have both the FileUpload and IO installed and working -
Any ideas?
Thanks
|
|
|
rajnish kumar
|
Posted: 01/19/2006, 8:11 PM |
|
i want to limit the file size to be uploaded on to the server. how to limit the file size.i am new to java. so please help me.
thanks
|
|
|
pfos.bmth
|
Posted: 01/20/2006, 1:24 PM |
|
Quote dingo:
It works great (for me) until it finishes the Process_FileUpload - and then every time no matter which file size, I get an "Errors prevented the file upload" every time..
I have both the FileUpload and IO installed and working -
Any ideas?
Thanks
add code as shown below and analyse the error message. post anything you dont understand back here and we'll see if we can help.
catch (java.lang.Exception e) {
out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br>");
out.println("Error message: " + e.getMessage + "<br>");
}
|
|
|
pfos.bmth
|
Posted: 01/20/2006, 4:27 PM |
|
That code should be:
catch (java.lang.Exception e) {
out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br>");
out.println("Error message: " + e.getMessage() + "<br>");
|
|
|
jarol
|
Posted: 01/23/2006, 11:45 AM |
|
Hi All,
I am working on a JAVA/JSP application.
Now what I want is when the user clicks on the link from html page the name of the file will be passed and, open/save dialog will appear which give flexibility to the user to download or view the file in JSP page.
Any help is highly appreciated. Thans in advance
|
|
|
dingo
Posts: 3
|
Posted: 01/24/2006, 11:17 AM |
|
OK that helped!!! Thanks so much, what a great help.
|
|
|
dingo
Posts: 3
|
Posted: 01/25/2006, 6:42 AM |
|
Thank you it is working great, I would like to know if it is possible to limit the file type by extension that users can upload? Where could I work that into the code?
|
|
|
prakash
|
Posted: 02/06/2006, 1:10 AM |
|
Hey im new to this forum .
I want to upload a single file with a big size and i used ur code ..
but im getting some exception
javax.servlet.ServletException: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.Process_005fFileUpload_jsp._jspService(org.apache.jsp.Process_005fFileUpload_jsp:162)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
can u please help me
Quote pfos.bmth:
Jakarta Commons FileUpload allows you to process parameters posted in the form along with files. It can process many files as well. Heres an example:
Process_FileUpload.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<html>
<head>
<title>Commons-FileUpload-1.1 Report Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%!
//method to return file extension
String getFileExt(String xPath){
//Find extension
int dotindex = 0; //extension character position
dotindex = xPath.lastIndexOf('.');
if (dotindex == -1){ // no extension
return "";
}
int slashindex = 0; //seperator character position
slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));
if (slashindex == -1){ // no seperator characters in string
return xPath.substring(dotindex);
}
if (dotindex < slashindex){ //check last "." character is not before last seperator
return "";
}
return xPath.substring(dotindex);
}
%>
<%
// Check that we have a file upload request
boolean isMultipart = FileUpload.isMultipartContent(request);
out.println("isMultipart="+isMultipart+"<br>");
//Create variables for path, filename and extension
String newFilePath = application.getRealPath("/") + "LOCATIONOFFILE";
String newFileName ="";
String FileExt = "";
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
//Form fields
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if (name.equals("newFileName")) {
newFileName = value;
}
out.println("Form Field Name= " + name + "<br>");
out.println("Form Field Value= " + value + "<br><br>");
}
else {
String fieldName = item.getFieldName();
String fileName = item.getName();
FileExt = getFileExt(fileName);
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println("fieldName= " + fieldName + "<br>");
out.println("fileName= " + fileName + "<br>");
out.println("FileExt= " + FileExt + "<br>");
out.println("contentType= " + contentType + "<br>");
out.println("isInMemory= " + isInMemory + "<br>");
out.println("sizeInBytes= " + sizeInBytes + "<br>");
if (fileName.equals("") || sizeInBytes==0){
out.println("Not a valid file.<br>No upload attempted.<br><br>");
} else {
File uploadedFile = new File(newFilePath+"/", newFileName+FileExt);
try{
item.write(uploadedFile);
out.println(fileName+" was successfully uploaded as "+ newFileName+FileExt +".<br><br>");
}
catch (java.lang.Exception e) {
out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br><br>");
}
}
}
}
%>
</body>
</html>
The above code will only work if the new filename is processed before the file.
Heres some sample html that will upload 3 files of any kind, using values posted in hidden fields to pass new filenames:
<body>
<form method="post" action="Process_FileUpload.jsp" name="uploadform" enctype="multipart/form-data">
<table >
<tr>
<td>Select up to 3 files to upload</td>
</tr>
<tr>
<td>
<input type="hidden" name="newFileName" value="newUploadedFileName1">
<input type="file" name="uploadfile" >
</td>
</tr>
<tr>
<td>
<input type="hidden" name="newFileName" value="newUploadedFileName2">
<input type="file" name="uploadfile" >
</td>
</tr>
<tr>
<td>
<input type="hidden" name="newFileName" value="newUploadedFileName3">
<input type="file" name="uploadfile" >
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
|
|
|
N Kishore
|
Posted: 02/15/2006, 2:15 AM |
|
hi,
iam gettin these error when iam running an program with servlet and jsp
jasperException:class cannot be complied
out.write("n\r\");...expected ";"
|
|
|
|