问题描述
- 出现下面错误怎么解决啊 ,求方法,请大神们帮我看看!
-
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:tomcatwebappsday10imggirl7.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); } }
解决方案
if(agent.contains("MSIE"))
->
if(agent.indexOf("MSIE") != -1)
这个支持更好
解决方案二:
是不是JDK版本问题啊,我用JDK1.8就没有这个问题啊。
解决方案三:
没有java.lang.CharSequence,你看一下是不是项目没有添加jre system library。检查一下jre运行环境。
时间: 2024-10-02 04:20:58