This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with Uploading file  RSS feed

 
Don Martino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to upload a tar file from local pc(c:\dir1\dir2\Upload.tar) to linux server. The html in first jsp is working:

<form name="browseforfile" action="/mau/jsp/Upload.jsp" method=POST ENCTYPE="multipart/form-data">
<%--<form name ="browseforfile" action="fsuwebupload" method=POST ENCTYPE="multipart/form-data">--%>
<input type=hidden name="anncFileName" value=''>
<table border="0">
<tr>
<td>

<strong>Announcement Batch Files test:</strong>

</td>
<td><input type="file" size="30" name="anncFile" accept="*/*"></td>
<td></td>
</tr>

<tr>
<td></td>
<td><img border="0" <br /> ></td>
<td></td>
</tr>


The problem is that the Upload.jsp is throwing an IndexOutOfBounds error because I'm somehow processing too many bytes. Could someone look at my code or could someone suggest a better mouse trap?



<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>
<%@ page import="java.io.FileWriter,java.io.IOException" %>
<%@ page import="com.att.vtone.mau.*"%>
<%@ page import="com.att.ivss.util.*" %>
<%@ page import="com.att.ivss.alarm.Alarm"%>
<%@ page import="com.att.ivss.commonweb.*" %>



<%
Log logger = LogFactory.getLog("mau");
logger.debug("Upload Helper ");

String savePath = "", filepath = "", filename = "";
String contentType = "", fileData = "", strLocalFileName = "";
int startPos = 0;
int endPos = 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 ];

}
}

%>

<%
int BOF = 0, EOF = 0;
contentType = request.getContentType();

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



//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)
startPos = 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);
endPos = count - boundary.length();
//file data extracted

//create destination path & save file there
String appPath = application.getRealPath("/");

//out.println("<br>appPath : " + appPath);
logger.debug("Upload Helper appPath= "+appPath);
String destFolder = appPath + "images/banner/";
filename= destFolder + filename;
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.write(dataBytes, startPos, (endPos - startPos));
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
{
logger.debug("Upload Helper Error in uploading");
//out.println("Error in uploading ");
}

%>

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP is a technology intended for creating text-based responses. Misusing it as a data processing engine is sure to cause problems.

Step 1: Move the logic out of the JSP and into a servlet. Then we can take it from there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!