阿里云OSS上文件下载后文件名前带下划线前缀问题的解决

背景:最近项目中,一个新人说自己写的文件上传和下载有点儿问题,什么问题呢,就是一个文件上传到阿里云OSS后,文件名都是正常的,但是从阿里云上下载之后,文件名前面代了一个下划线(_),有点儿莫名其妙。

如图:

研究了一下他的代码,之后,发现有点儿问题,具体说就是没有设置正确的contentType. 全部设置成了file。

解决方法如下:

设置不同的contentType

/**
   * Description: 判断OSS服务文件上传时文件的contentType
   * @param filenameExtension 文件后缀
   * @return String
   */
  public static String getContentType(String filenameExtension) {
	    String contentType = "";
	  	switch(filenameExtension.toUpperCase()) {
	  		case "BMP": contentType = "image/bmp";break;
	  		case "GIF": contentType = "image/gif";break;
	  		case "JPEG":
	  		case "JPG": 
	  		    contentType = "image/jpg";
	  		    break;
	  		case "PNG": contentType = "image/png";break;
	  		case "HTML": contentType = "text/html";break;
	  		case "TXT": contentType = "text/plain";break;
	  		case "VSD": contentType = "application/vnd.visio";break;
	  		case "PPTX":
	  		case "PPT": contentType = "application/vnd.ms-powerpoint";break;
	  		case "DOCX": contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";break;
	  		case "DOC": contentType = "application/msword";break;
            case "XLSX": contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";break;
	  		case "XLS": contentType = "application/vnd.ms-excel";break;
	  		case "XML": contentType = "text/xml";break;
	  		case "PDF": contentType = "application/pdf";break;
	  		default:contentType="file";
	  	}
	    return contentType;
  }

需要根据具体的文件类型来设置contentType,而不是全部设置为file

设置之后,上传文件到阿里云OSS,再下载,完美解决!

如果对你有帮助,记得点赞关注转发哦

猜你喜欢

转载自blog.csdn.net/wangerrong/article/details/128173386