日期:2014-05-19  浏览次数:20716 次

java开发的一个问题在windows无问题部署到linux就不行是为什么
java开发的一个问题在windows无问题部署到linux就不行是为什么

情况是这样 :
在windows开发的程序一个下载excel的功能 在webroot下有个文件夹 excel 里面装着 a.xls 

这个功能就是从服务器把这个a.xls下载到本地 ,程序在windows下部署了测试了 功能可以用 无错

但是把项目部署到linux上 发现下载的a.xls 提示格式不对是什么原因 一样的程序 一样的代码 一样的xls啊

下载代码如下:
response.setContentType("text/html;charset=utf-8");  
  try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}  
  java.io.BufferedInputStream bis = null;  
  java.io.BufferedOutputStream bos = null;  
  
  String ctxPath = request.getSession().getServletContext().getRealPath("/")+ "\\excel\\";  
  String fileName = "user.xls";
  String downLoadPath = ctxPath +fileName;  
  try {  
  long fileLength = new File(downLoadPath).length();  
  response.setContentType("application/x-msdownload;");  
  response.setHeader("Content-disposition", "attachment; filename="  
  + new String(fileName.getBytes("utf-8"), "ISO8859-1"));  
  response.setHeader("Content-Length", String.valueOf(fileLength));  
  bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
  bos = new BufferedOutputStream(response.getOutputStream());  
  byte[] buff = new byte[2048];  
  int bytesRead;  
  while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
  bos.write(buff, 0, bytesRead);  
  }  
  } catch (Exception e) {  
  e.printStackTrace();  
  } finally {  
  if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}  
  if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}  
  }  


------解决方案--------------------
这个我知道,我也遇到过
------解决方案--------------------
是不是因为linux上的路径是以 / 区分的。。

\\excel\\ 改成 /excel/ 试试呢。。
------解决方案--------------------
给你个链接自己看http://blog.csdn.net/deng11342/article/details/7479136