使用content-type响应头(服务器发送内容的类型和编码类型)

package cn.itcast.web.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

//通知浏览器打开文件的类型
response.setHeader("content-type","image/jpeg");
//response.setHeader("content-type","application/msword");


File file = new File("d:\\a.jpg");
InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();//对应浏览器的输出流
byte[] buf = new byte[1024];
int len = 0;
while( (len=is.read(buf))>0 ){
os.write(buf,0,len);
}
is.close();
os.close();
}
}


你可能感兴趣的:(http相关知识)