Hi. I have this servlet for file uploading.
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class myUploadServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException {
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException {
String contentType = request.getContentType();
if ((contentType != null)
&& (contentType.indexOf("multipart/form-data")
>= 0)) {
DataInputStream in;
byte dataBytes[];
try {
in = new
DataInputStream(request.getInputStream());
// we are taking the length of Content type data
int formDataLength = request.getContentLength();
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;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
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("\""));
//System.out.println("saveFile = " + saveFile);
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 file
FileOutputStream fileOut;
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String path = "Images" + File.separator + saveFile;
try {
fileOut = new FileOutputStream(path);
fileOut.write(dataBytes, startPos, (endPos -
startPos));
fileOut.flush();
fileOut.close();
} catch (FileNotFoundException e) {
response.setContentType("text/html");
response.setContentLength(e.getMessage().length());
out.println( /* e.getMessage() */"File Not
Found");
// e.printStackTrace();
return;
} catch (IOException e) {
response.setContentType("text/html");
response.setContentLength(e.getMessage().length());
out.println( /* e.getMessage() */"Problem
reading the file");
// e.printStackTrace();
return;
}
File f = new File(path);
if (f.length() == 0) { // empty file
f.delete();
try {
out = response.getWriter();
} catch (IOException e1) {
e1.printStackTrace();
}
response.setContentType("text/html");
String msg = "File is empty or doesnt exist";
response.setContentLength(msg.length());
out.println(msg);
} else {
response.setContentType("text/html");
String msg = "File: " + f.getName();
response.setContentLength(msg.length());
out.println(msg);
}
}
}
}
It seems correct to my eyes but i get this error:
<h2>HTTP ERROR: 500</h2><pre>INTERNAL_SERVER_ERROR</pre>
<p>RequestURI=/myfrwa/upload</p><h3>Caused by:</
h3><pre>java.lang.IndexOutOfBoundsException
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at fr.server.myUploadServlet.doPost(myUploadServlet.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:
487)
at org.mortbay.jetty.servlet.ServletHandler.handle
(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle
(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle
(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle
(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:
405)
at org.mortbay.jetty.handler.HandlerWrapper.handle
(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle
(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle
(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:
505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content
(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
at org.mortbay.jetty.HttpConnection.handl
Anyone who can tell me what is going on?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---