if(agent.contains("MSIE")){
這句報錯, 錯誤提示The method contains(CharSequence) from the type String refers to the missing type CharSequence
package cn.itcast.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 文件下載
* @author Administrator
*
*/
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 先獲取到文件,讀入輸入流中
// 獲取文件的絕對磁盤路徑
String path = getServletContext().getRealPath("/img/外國美女.jpg");
// 包含文件的名稱 c:\tomcat\webapps\day10\img\girl7.jpg
// 截取文件的名稱 girl7.jpg
String filename = null;
// 獲取最後一個\的位置
int index = path.lastIndexOf("\\");
if(index != -1){
filename = path.substring(index+1);
}
// 判斷是什麼浏覽器 Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
// Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
String agent = request.getHeader("User-Agent");
// System.out.println(agent);
if(agent.contains("MSIE")){
filename = URLEncoder.encode(filename,"UTF-8");
}
if(filename != null){
// 設置頭信息
response.setHeader("Content-Disposition", "attachment;filename="+filename);
System.out.println(request.getRemoteAddr());
System.out.println(path);
InputStream in = new FileInputStream(path);
// 通過response讀出到客戶端
OutputStream os = response.getOutputStream();
// io的拷貝
byte [] b = new byte[1024];
int len = 0;
while((len = in.read(b)) != -1){
os.write(b, 0, len);
}
in.close();
// os.close();服務器管理流
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
參數類型不對,它需要的是一個CharSequencel類型的參數