Encoding in java8 spring boot app. French character é is displaying as Ac in File name while downloading the file. File content is correctly Encoded

Certainly! Here’s your question formatted for submission on Stack Overflow:


Description:

In my Java 8 Spring Boot application, I’m encountering an encoding issue with French characters. Specifically, the character ‘é’ is displayed as ‘Ac’ in downloaded file names and response headers. This issue persists even when inspecting the network trace in the browser. Below is the original code snippet:

Service Class:

public ResponseEntity<?> downloadReport(Long serviceId, Long reportId) throws Exception, IOException {
    // Retrieve file details from cache
    DownloadFileCache downloadFileCache = FileCacheUtil.getFileDetailsFromCache(reportId);
    String reportStatus = "DOWNLOADED";
    
    if (!ObjectUtils.isEmpty(downloadFileCache)) {
        try {
            // Get the file information based on documentId
            String fileName = downloadFileCache.getFileName();
            String path = downloadFileCache.getPath();
            String actualFileName = null;

            if (StringUtils.isBlank(path) || StringUtils.isBlank(fileName)) {
                Status status = buildStatus(GetReportSchedulesRequestValidator.BAD_REQUEST_STATUS_CODE,
                        Severity.Error, "File not found.");
                reportStatus = null;
                throw new Exception(status);
            }

            if (StringUtils.isNotBlank(fileName)) {
                actualFileName = fileName.substring(0, fileName.lastIndexOf("•"));
            }

            HttpHeaders httpHeaders = new HttpHeaders();

            String mimeType = URLConnection.guessContentTypeFromName(actualFileName);
            if (mimeType == null) {
                mimeType = "application/octet-stream";
            }
            httpHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="" + actualFileName + """);
            httpHeaders.setContentType(MediaType.parseMediaType(mimeType));

            if (StringUtils.isNotBlank(fileName) && fileName.contains("&")) {
                fileName = fileName.replace("&", "%26");
            }
            Log.info("httpheaders- " + httpHeaders);
            Log.debug("httpheaders= " + httpHeaders);
            
            return ResponseEntity.ok()
                    .headers(httpHeaders)
                    .body(Client.download(path, fileName).getBody());
                    
        } catch (Exception e) {
            Status status = buildStatus(GetReportSchedulesRequestValidator.BAD_REQUEST_STATUS_CODE,
                    Severity.Error, "File not found.");
            reportStatus = null;
            throw new Exception(status);
        } finally {
            if (StringUtils.isNotBlank(reportStatus)) {
                updateDownloadStatus(reportId, serviceId, reportStatus);
            }
        }
    }
    return null; // Handle case when downloadFileCache is empty
}

Client Code:

public ResponseEntity<ByteArrayResource> download(String containerName, String blobName) throws Exception, IOException {
    Log.trace("inside download documents");
    RestRequestBuilder requestBuilder = RestRequestBuilder.getRequest()
            .withPathSegment(containerName)
            .withPathSegment(PREFIX)
            .withQueryParam(BLOB, blobName);
    return ResponseEntity.ok().body(callForEntity(requestBuilder, ByteArrayResource.class).getBody());
}

Original code works fine in Linux but it is not working in Microsoft cloud giving encoding issues.
Issue Resolution Attempt:

To address the encoding issue, I attempted to forcefully encode the file name using UTF-8 in the service class:

String encodedFilename = URLEncoder.encode(actualFileName, StandardCharsets.UTF_8.toString());
httpHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="" + encodedFilename + """);

However, during debugging, I observed that with the original code, the request headers had the correct file name. But with the above approach, instead of spaces, ‘%c3%a9’ was added, and in the response dump, ‘CONTENT_DISPOSITION’ filename contained ‘Ac’ characters instead of the French character ‘é’.


I’m expecting this french character to be correctly displayed in downloaded file name

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật