Hi.
I have a JSP uploader. This works fine on Windows platforms. The linux version works well too, but only with text files. Any other file (.gpeg, .odt, .pdf etc) get corrupted in some way or another on uploading.
Kindly look at the code involved and help me out. Thanks.
<%@ page import="java.io.*, java.lang.*" %>
<%
out.println("Uploading your file. Please wait...");%><br><%
String operatingSystemVersion = String.valueOf(System.getProperty("os.name"));
FileOutputStream fileOut = null;
if(operatingSystemVersion.startsWith("Linux"))
{
String contentType = request.getContentType();
String file = null;
String saveFile = "";
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;
}
try
{
file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("/") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=/" + 1);
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;
out.print("Boundary: " + boundaryLocation + " ");
int startPos = ((file.substring(0, pos)).getBytes()).length;
out.print("startPos: " + startPos + " ");
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
out.print("endPos: " + endPos + " ");
//Specify the folder here that you want the file uploaded into
// Note that you need read/write permissions to that folder
String folder = "/home/arthur/uploads/";
fileOut = new FileOutputStream(folder + saveFile);
for(int i = 0; i < dataBytes.length; i++)
{
byte data = dataBytes;
fileOut.write(data);
}
fileOut.write(dataBytes, startPos, (endPos - startPos));
// out.print("Writing out OK");
out.print("flushing...");
fileOut.flush();
//
// here </TD>
// <meta HTTP-EQUIV="Refresh" content="5; URL=index.jsp">
// <br>
//
}
catch(Exception e)
{
out.print(e);
}
}
}
else
{
String saveFile = "";
String contentType = request.getContentType();
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;
}
try
{
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
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;
// Specify the folder here that you want the file uploaded into
// Note that you need read/write permissions to that folder
String folder = "C:/Program Files/Apache Software Foundation/Tomcat5.5/webapps/upload/images/";
fileOut = new FileOutputStream(folder + saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
<meta HTTP-EQUIV="Refresh" content="5; URL=index.jsp">
<%
}
catch(Exception e)
{
out.print(e);
}
finally
{
try{fileOut.close();}catch(Exception err){}
}
}
}
%>
{code}