Uploading different files to tomcat server
843836Mar 11 2005 — edited Mar 21 2005HI all,
I am uploading a .csv file to tomcat server using this code�(assume that I am uploading �employee.csv� file)
code
****************************************************************
<%
String contentType = request.getContentType();
out.println("\n \nContent type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
out.println("\n \nDataInputStream="+in);
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.println("\n \n databytes"+dataBytes);
out.println("saveFile="+saveFile);
int lastIndex = contentType.lastIndexOf("=");
out.println("\n \n lastIndex="+lastIndex);
String boundary = contentType.substring(lastIndex + 1,contentType.length());
out.println("\n \n boutndary="+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 in the server as " +saveFile);
}
%>
*****************************************************************
In the above code, �saveFile� will have the name of the file, which has been uploaded to server.
In the following code, I am reading a file which is uploaded in the server (reading from the server). For that I should give the path, like where it has been saved���
Eg. �C:/program files/Apache Tomcat 4.0/�/�/employee.csv�
code
************************************************************************
<%-- Import the necessary Java classes --%>
<%@ page import="java.text.*,java.sql.*,java.util.*,javax.servlet.*,javax.servlet.http.*,javax.servlet.http.HttpSession,java.util.Properties,java.util.StringTokenizer,java.io.*,java.awt.*,javax.mail.*,javax.mail.Message,javax.activation.*,javax.mail.Session,javax.mail.Transport,javax.mail.internet.*,javax.mail.internet.MimeMessage,javax.mail.internet.InternetAddress" %>
<%@ page contentType="text/html;charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
try
{
String s1="";
boolean status=true;
FileInputStream fstream = new FileInputStream("c://Documents and Settings/in900055/start menu/programs/apache tomcat 4.0/employee.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
---------------
--------------
-------------
----------------
}catch(Exception e)
{
}
********************************************************
My problem is, if I use these two programs to upload a file and read that file from server, each time I have to save the file as �employee� only�..B�coz that file name is included in the path��But, the program should work even if I upload different .csv files with different file name�..how to do that ??
I can't concat the file name....i tried in vain......it gives error.
Please help !!!!!