praveenog
Posts: 1
|
Posted: 06/28/2007, 2:18 AM |
|
hi
<%
String url="http:| \\-/ |"+request.getLocalName()+":"+request.getLocalPort()+request.getContextPath()+"/images/";
String contentType = request.getContentType();
System.out.println("444444444444444444 "+url);
System.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();
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.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;
//out.println(url);
String demo="http://localhost:8080/challenge/images/200.jpg";
System.out.println("http://localhost:8080/challenge/images/200.jpg");
FileOutputStream fileOut = new FileOutputStream("326.jpg");
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
//out.println("File saved as " +saveFile);
}
%>
hi this code is working for me but it wil save the image in webapps bib directory but i need to save image in the images directory . any way hard codeing like this is not possible "http://localhost:8080/challenge/images/200.jpg.. i need to save this using request.getContextPath any one can help me
|
|
|
matheus
Posts: 386
|
Posted: 06/28/2007, 4:15 AM |
|
Codecharge relationship?
_________________
Matheus Trevizan
Dynamix Software Ltda.
Blumenau SC Brasil
www.dynamix.com.br |
|
|
varun
Posts: 1
|
Posted: 07/02/2007, 9:51 PM |
|
I have gone through the code, but can anyone tell me how to store the file in the db server(ie to store the file in the server directory)
as the code wich are given store it in the client machime itself.
I am confused where to give the actual url of the server.......do reply
|
|
|
fady005
Posts: 115
|
Posted: 07/03/2007, 6:37 AM |
|
Just one question, is your db server the same as your JSP server ?
If not I can't tell you how to do it because the upload happen from the JSP client to the JSP server regardless to the db Server...
_________________
Be inventive ! |
|
|
senthil_sivanath
Posts: 5
|
Posted: 12/28/2007, 10:34 AM |
|
<html>
<head></head>
<body>
<form action="multipart2.jsp" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
<td><INPUT type="text" name='username'> </td>
</tr>
<tr>
<td align="left">
<input type="file" name="filename" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
////////////////////////////here is the multipart2.jsp
<%@ page import="java.util.*,java.io.*"%>
<%
String path=request.getParameter("file");
String newPath="";
int count=0;
if(path!=null)
{
ArrayList arr=new ArrayList();
StringTokenizer st=new StringTokenizer(path,"\\");
while(st.hasMoreTokens())
{
arr.add(count,st.nextToken());
count++;
}
// create ur own path
newPath=config.getServletContext().getRealPath("/")+"/attachments/"+arr.get(count-1);
int c;
FileInputStream fis=new FileInputStream(path);
FileOutputStream fos=new FileOutputStream(newPath);
while((c=fis.read())!=-1)
{
fos.write((char)c);
}
}
out.println("Thanks for using");
out.println("<br>");
out.println("<br>");
out.println("1.File1 Uploaded from :: "+path);
out.println("<br>");
out.println("<br>");
out.println("2.Uploaded File1 is Saved in :: "+newPath); %>
In this code attachments is the folder name Change it to images...........if u need
_________________
Arise, Awake and Stop not till your goal is reached |
|
|
|