Content-Type用於定義網絡文件的類型和網頁的編碼,決定瀏覽器採用何種方式對響應體進行處理。HttpServletRequest提供了一個專門的方法setContentType()來設置Content-Type
MIME類型 | 描述 |
---|---|
text/html | HTML格式 |
text/plain | 純文本格式 |
text/xml | XML格式 |
image/jpg | jpg圖片格式 |
image/png | png圖片格式 |
image/git | git圖片格式 |
multipart/form-data | 表單進行文件上傳時使用的格式 |
application/octet-stream 或 application/x-msdownload 或 Content-Disposition (attachment / inline) |
二進制流數據(如常見的文件下載) |
. . . . . . | . . . . . . |
HTML文檔實例
/**
* @Author: 落葉無痕
* @Date: 2020/6/2 17:03
*/
@WebServlet("/ct")
public class UserAgentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設置Content-Type內容類型
response.setContentType("text/html;charset=utf-8");
//瀏覽器打印輸出
response.getWriter().println("<h1><a href='http://caidong4356.top/'>Caidong的個人博客</a></h1>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
}
}
純文本實例
/**
* @Author: 落葉無痕
* @Date: 2020/6/2 17:03
*/
@WebServlet("/ct")
public class UserAgentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設置Content-Type內容類型
response.setContentType("text/plain;charset=utf-8");
//瀏覽器打印輸出
response.getWriter().println("<h1><a href='http://caidong4356.top/'>Caidong的個人博客</a></h1>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
}
}
文件下載實例
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
/**
* @Author: 落葉無痕
* @Date: 2020/6/3 16:32
*/
@Controller
public class UploadController {
@GetMapping("/download/{filename}")
public void downloadFile(@PathVariable("filename") String filename, HttpServletResponse response) throws UnsupportedEncodingException {
if(filename != null){
//獲取要下載的文件對象
File file = new File("D:/jianli/", filename);
if(file.exists()){
//以二進制流的形式下載文件(這樣可以實現任意格式的文件下載)
response.setContentType("application/x-msdownload");
// response.setContentType("application/octet-stream);
InputStream input = null;
OutputStream output = null;
try{
input = new FileInputStream(file); //創建文件輸入流對象
output = response.getOutputStream(); //創建文件輸出流對象
byte[] buf = new byte[1024]; //定義一個字節數組作爲緩衝區,並指定一次讀取1024個字節
int len; //記住讀入緩衝區的字節長度
while ((len = input.read(buf)) != -1){ //判斷文件是否讀完(讀完後會返回-1)
//注意不要用output.write(buf); 否則可能導致最終寫入的文件比原文件大,如果文件是圖片的話,那麼就會失真
output.write(buf, 0, len); //從buf緩衝區中讀取數據,從第一個字節開始讀,一次讀取len,並將讀取出來的數據寫入文件輸出流對象中
}
}catch (Exception e){
e.printStackTrace();
}finally {
//最後記得關閉IO流
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}