熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

文件下載程序中文件名過長的問題

2022-06-13   來源: Java核心技術 

  今天測試文件下載程序中發現的文件名過長的問題 居然發現文件名編碼後長度超過就會不能正確顯示和下載最後只好找了這樣一個折中的方法截短了
  下面是那裡的代碼
  /// <summary>
  /// 下載附件
  /// </summary>
  /// <param name=fileName>文件名</param>
  /// <param name=path>文件路徑</param>
  public static void DownLoadFileAttachment(string fileName string path)
  {
  if (SystemIOFileExists(path))
  {
  try
  {
  fileName = fileNameTrim();
  
  for (int i = ; i < SystemIOPathInvalidPathCharsLength ; i ++)
  {
  fileName = fileNameTrim()Replace(SystemIOPathInvalidPathChars[i]ToString() stringEmpty);
  }
  
  fileName = fileNameReplace(SystemIOPathPathSeparatorToString() stringEmpty);
  
  int maxLength = ;
  
  int length = HttpUtilityUrlEncode(fileName)Length;
  while (length > maxLength)
  {
  int index = fileNameLastIndexOf();
  if (index > )
  {
  fileName = fileNameSubstring( index ) + fileNameSubstring(index);
  }
  else
  {
  fileName = fileNameSubstring( fileNameLength );
  }
  length = HttpUtilityUrlEncode(fileName)Length;
  }
  
  SystemIOFileInfo file = new SystemIOFileInfo(path);
  HttpContextCurrentResponseClear();
  HttpContextCurrentResponseAppendHeader(ContentDisposition attachment; filename= + HttpUtilityUrlEncode(fileName));
  HttpContextCurrentResponseAppendHeader(ContentLength fileLengthToString());
  HttpContextCurrentResponseContentType = application/octetstream;
  HttpContextCurrentResponseWriteFile(fileFullName);
  HttpContextCurrentResponseEnd();
  }
  catch
  {
  }
  }
  else
  {
  HttpContextCurrentResponseClear();
  DisplayNoFileMessage();
  HttpContextCurrentResponseEnd();
  }
  }

From:http://tw.wingwit.com/Article/program/Java/hx/201311/27168.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.