hello sir..
i have this jsp page ...which i want to use for image upload of a user...
i wannt to post the user no as id here along with the image file to another page where it will be saved in DB and on the server as well..
<%@ page language="java" import="java.sql.*,java.util.*,java.io.*" %>
<html>
<body bgcolor="#FFFFFF" leftmargin="0" rightmargin="0" marginheight="0" marginwidth="0">
<jsp:include page="header.jsp" flush="true" />
<%
String n = request.getParameter("id");
//out.println(n);
%>
<table align="center" border="0">
<form action="insertuserpic.jsp" method="post" enctype="multipart/form-data">
<tr><th>Select your picture :</th></tr>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input type="submit" name="submit" value="upload"></td></tr>
<input type="hidden" name="id" value="<%= n %>">
</form>
</table>
</body>
</html>
now insertuserpic.jsp is here:
<%@ page
import="java.sql.*,java.util.*,java.io.*" %>
<%
String n=request.getParameter("id");
out.println(n);
String contentType = request.getContentType();
out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
out.println("content length is"+formDataLength+"\n");
byte dataBytes[] = new byte[formDataLength];
out.println("databytes :"+dataBytes+"\n");
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
out.println("file : "+file);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(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("C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/project/userpics/" + saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " +saveFile);
}
file gets uploaded all well...but m not able to get the ID ...it comes null..
i have downloaded the com.oreilly.servlet.multipart library..
but i dont know how to use it..
could anyone just provide the exact code to get the id as well as the image...
i found the above code also on net....
can anyone help please...