the question description:
when I read a jpg file, there is no any problem. but when I try to read a mht file, the service method was run twice. why? Can anyone give me a satisfying explain? Thanks first in here.
public class ShowImageServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
getImageTask(request,response);
}
public void getImageTask(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
OutputStream toClient=null;
int maxFileSize=1048576;
String genPath= "D:\\";
String path="";
String realPath="";
String imageType="";
String finalPath="";
FileInputStream inFile=null;
byte[] data ;
try{
path=request.getParameter("filePath");
if(path==null||"".equals(path))
{
throw new Exception("you send a empty file");
}
else{
int splitType=path.indexOf(".");
imageType=path.substring(splitType+1,path.length());
int splitBank=path.indexOf("_");
if(splitBank!=-1){
realPath=path.substring(0,splitBank);
finalPath=genPath+File.separatorChar+"advert"+File.separatorChar+realPath.trim()+File.separatorChar+path;
}else{
StringTokenizer toke = new StringTokenizer(path, "|");
while (toke.hasMoreElements()) {
String pathName = toke.nextToken();
realPath=realPath+File.separator+pathName;
}
finalPath=genPath+realPath.trim();
}
}
if(("gif").equals(imageType))
{
imageType="image/gif;charset=GB2312 ";
}
else if(("swf").equals(imageType)){
imageType="application/x-shockwave-flash;charset=GB2312 ";
}
else if(("mht").equals(imageType)){
imageType="message/rfc822;charset=GB2312 ";
}
else{
imageType="image/jpeg;charset=GB2312 ";
}
response.setContentType( imageType); //
System.out.println(finalPath+"...."+imageType);
File file = new File(finalPath);
if(file.exists()&&file.isFile()){
inFile = new FileInputStream(file); //
int i = inFile.available(); //
if(i>maxFileSize){
throw new Exception( "file size is too large");
}
data= new byte[i];
inFile.read(data); //
inFile.close();
} else{
String emptyPath=genPath+File.separatorChar+"empty.jpg";
File emFile = new File(emptyPath);
inFile = new FileInputStream(emFile); //
int i = inFile.available(); //
data = new byte[i];
inFile.read(data); //
inFile.close();
//inFile.reset();
inFile = null;
}
toClient = response.getOutputStream(); //
toClient.write(data); //
toClient.flush();
toClient.close();
toClient =null;
}
catch (IOException ex) {
System.err.println("****************Error1");
ex.printStackTrace();
throw ex;
}catch(Exception e){
System.err.println("****************Error1");
}
finally {
if (toClient != null) {
try {
toClient.flush();
toClient.close();
}
catch (IOException e2) {
System.err.println("****************Error1");
}
}
}
}
}
<%@ page language="java" contentType="text/html; charset=GBK" %>
<html>
<head>
<title>My Test </title>
</head>
<script>
function test(){
var file = document.getElementById('testfile').value;
var theUrl = '/mytest/ShowImage?filePath='+file;
window.open(theUrl);
}
</script>
<body>
<form name="form1">
<table>
<tr>
<td>
<input type="text" id="testfile" name="filePath" value="t.mht">
<input type="button" value="upfile" onclick="test();">
</td>
</tr>
</table>
</form>
</body>
</html>