Solve the problem of the underscore prefix before the file name after the file is downloaded on Alibaba Cloud OSS

Background: In a recent project, a newcomer said that there was a problem with the upload and download of the file he wrote. What is the problem? After a file is uploaded to Alibaba Cloud OSS, the file name is normal, but after downloading from Alibaba Cloud, the file name is not There is an underscore (_) in front of the name, which is a bit baffling.

As shown in the picture:

After studying his code, I found some problems. Specifically, the correct contentType was not set. All were set to file.

The solution is as follows:

Set a different 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;
  }

The contentType needs to be set according to the specific file type, not all set to file

After setting, upload the file to Alibaba Cloud OSS, and then download it, perfect solution!

If it is helpful to you, remember to like, follow and forward

Guess you like

Origin blog.csdn.net/wangerrong/article/details/128173386