Results 1 to 12 of 12
- 08-10-2010, 04:15 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
- 08-10-2010, 04:47 PM #2
Are you looking to do this in a Java(Servlet) or Javascript?
There is a massive difference between each.:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 08-11-2010, 07:30 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
yes i want to do this in java servlet
- 08-11-2010, 08:34 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Might I suggest Google?
There's rather a lot of stuff out there for file uploading in servlets.
- 08-11-2010, 12:39 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
i tried but i didn't get my solution
if you have program then please give me
- 08-11-2010, 12:49 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
- 08-13-2010, 06:15 AM #7
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
I have following code but it's not working :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and
//as well as the passed data from mulitpart
//form - data
// is greater than or
//equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form - data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,
formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
String 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;
//extracting the index of file
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;
// creating a new file with the same name and writing the content in =new fileFileOutputStream
OutputStream fileOut = new FileOutputStream("E:/"+saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br>
<table border="2">
<tr>
<td>
<b>You have successfully
upload the file by the name of:</b>
<% out.println(saveFile);%>
</td>
</tr>
</table> <%}%>
</body>
- 08-13-2010, 09:38 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
That's not a servlet...it's a JSP.
You shold only have display code in a JSP.
Debugging that to find out what's wrong with it is a lot harder as a JSP.
But I will ask why you are turning a perfectly useable InputStream and turning it into a DataInputStream?
- 08-13-2010, 10:37 AM #9
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
yup i know that's not a servlet.
i find this from google.
- 08-13-2010, 11:10 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Why didn't you simply go with the Apache stuff which is the first link?
And it has code and everything...
- 11-16-2010, 11:21 AM #11
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
have look on
Uploding Image in jsp, Java
- 11-16-2010, 11:32 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Similar Threads
-
Ajax / servlet File upload
By n3mesi83 in forum Java ServletReplies: 0Last Post: 06-08-2010, 02:48 PM -
upload and download file using servlet
By amit_m04 in forum Java ServletReplies: 1Last Post: 09-27-2009, 03:06 AM -
Upload file to servlet and pass file to another servlet
By hofsoc in forum Advanced JavaReplies: 6Last Post: 06-04-2009, 11:09 PM -
File Upload Servlet problem
By jeniramires in forum Advanced JavaReplies: 3Last Post: 08-18-2008, 07:34 PM -
how to upload a file using servlet
By simon in forum Java ServletReplies: 1Last Post: 08-05-2007, 03:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks