473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

code for upload(pleas reply within 2 days)

sir,i have written the code for file uploading but it is very slow like it takas 2 to 3 minutes for 5 kb file and for 1 mb abt 20 minutes.so here is my code please help me for fast uploading also the code for download is given but it has some error help me in this because i have to submit after 3 days...help

[HTML] <html>
<body>
<%
String str=(String)session.getAttribute("cn");
//out.println(str);
if(str==null)
{
%>
<script language="javascript">
alert("Unauthorised Entry" + '/n' + "Please Login");
location.href="Faculty Login.jsp";
</script>
<%
}

%>
<%@ page import="java.io.*,javax.servlet.http.HttpServletRe quest,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[i];
}
}
%>
<%
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 + ""; //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 ");
}
%>
<script language="javascript">
alert("File Successfully Uploaded");
location.href="Faculty Welcome.jsp"
</script>
</body>
</html>
[/HTML]
Mar 31 '07 #1
0 1069

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Clifton M. Bean | last post by:
First, I defined three classes (listed below): =========== // 1st class =========== class PointCl { public: PointCl & operator= (const PointCl & rgh ); //define as usual assingment operator
5
by: genc ymeri | last post by:
Hi overthere, I'm trying to compile some Java code within a C# application form. So the end user will write his/her code in a textbox and a button compile will compiles it. I tried to use...
7
by: melinda | last post by:
What are some possible reasons for this: I call a static method from a class within my assembly...works fine. I call the same static method after it has been compiled into a different assembly...
3
by: Marty | last post by:
Hi, Can I mix my managed code within my unmanaged code? Just for instance, I have a socket class that is managed code. It is include in my unmanaged VC++ project and it compile. The point...
4
by: Dino Buljubasic | last post by:
Is it possible to use C# code within VB.net or viceversa and if yes why would one need to do somehting like this? Also, what would be advantages of C# over VB.net and viceversa? Thank you...
1
by: nesster13 | last post by:
Dear All, I am a new VB programmer and dont know very much about it so please bare with me. Here is the situation. My professor gave us an e-commerce website to work on but the problem is that...
2
by: prakashsakthivel | last post by:
What I want to know is that while one excel application is closed by VBA code from module, another excel application has to be opened. That means, I have written code to open new excel sheet in the...
3
by: Mike P | last post by:
I have a HTML menu that I want to add some C# code around which will include a variable declaration and an if statement. How do I add this code within my HTML? *If statement here, if true then...
3
by: mpjones2 | last post by:
I'm trying to organize some code I've written and was wondering if anyone can provide any suggestions on an equivalent to #Region that can be used within a function to collapse sections of code...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.