E
Edward
I have a simple servlet that reads a GIF from local machine and then
saves this file and displays the image in the browser. My problem is
that the image gets corrupted somehow. Looking at the file in Notepad
I see that the bytes are slightly different (resulting in a much
different image). I get a similar problem loading a JPEG but BMP looks
unchanged. I suspect that there is something I need to do with the
encoding but I am clueless in this regard. I think that perhaps
something needs to be done with the line:
InputStreamReader isr = new InputStreamReader(fis,"windows-1252");
but I have been unable to find any decent web resouce talking about
encoding a GIF. Or perhaps I am way off base and that I why I defer to
the local wisdom in the NG!
The servlet in its entirety:
package testPkg;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OutImage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
FileInputStream fis = new FileInputStream("C:\\test.gif");
InputStreamReader isr = new InputStreamReader(fis,"windows-1252");
Reader in = new BufferedReader(isr);
int ch;
StringBuffer buffer = new StringBuffer();
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
in.close();
String s = buffer.toString();
File outputFile = new File("C:\\testOUT.gif");
FileOutputStream fo = new FileOutputStream(outputFile);
fo.write(s.getBytes());
fo.close();
ServletOutputStream sos = response.getOutputStream();
sos.write(s.getBytes());
}
}
saves this file and displays the image in the browser. My problem is
that the image gets corrupted somehow. Looking at the file in Notepad
I see that the bytes are slightly different (resulting in a much
different image). I get a similar problem loading a JPEG but BMP looks
unchanged. I suspect that there is something I need to do with the
encoding but I am clueless in this regard. I think that perhaps
something needs to be done with the line:
InputStreamReader isr = new InputStreamReader(fis,"windows-1252");
but I have been unable to find any decent web resouce talking about
encoding a GIF. Or perhaps I am way off base and that I why I defer to
the local wisdom in the NG!
The servlet in its entirety:
package testPkg;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OutImage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
FileInputStream fis = new FileInputStream("C:\\test.gif");
InputStreamReader isr = new InputStreamReader(fis,"windows-1252");
Reader in = new BufferedReader(isr);
int ch;
StringBuffer buffer = new StringBuffer();
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
in.close();
String s = buffer.toString();
File outputFile = new File("C:\\testOUT.gif");
FileOutputStream fo = new FileOutputStream(outputFile);
fo.write(s.getBytes());
fo.close();
ServletOutputStream sos = response.getOutputStream();
sos.write(s.getBytes());
}
}