I'm in the process of revamping a 20-year-old web application. One of the pieces of the application that has not been used in a long time is a file uploader. The Java servlet that parses the multipart upload includes the following code, that worked with all browsers long ago and still works with Firefox:
This code does not work with Chrome, because Chrome starts its multipart/form-data boundary with "----WebKitFormBoundary" instead of "-----------------------------".
So I changed the servlet as follows:
The results have not changed.
Does anyone see anything obvious that I'm doing wrong?
Code:
String boundary = "-----------------------------"; ServletInputStream in = req.getInputStream(); byte[] bytes = new byte[512]; int state = 0; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); String name = null, value = null, filename = null, contentType = null; Dictionary fields = new Hashtable(); //Read a line from the request input stream int i = in.readLine(bytes,0,512); while (-1 != i) { String st = new String(bytes,0,i); //Beginning of parameter - write buffer from previous parameter if (st.startsWith(boundary)) {
So I changed the servlet as follows:
Code:
String boundary = "-----------------------------"; String boundary2 = "----WebKitFormBoundary"; ServletInputStream in = req.getInputStream(); byte[] bytes = new byte[512]; int state = 0; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); String name = null, value = null, filename = null, contentType = null; Dictionary fields = new Hashtable(); //Read a line from the request input stream int i = in.readLine(bytes,0,512); while (-1 != i) { String st = new String(bytes,0,i); //Beginning of parameter - write buffer from previous parameter // if (st.startsWith(boundary)) { if (st.startsWith(boundary) || st.startsWith(boundary2)) {
Does anyone see anything obvious that I'm doing wrong?
Comment