I have basic Spring app which takes an URL from user and gets spesific download link from website. It uses Jsoup and playwright in the background. The returned link is mp4 so it won’t start to download automatically on mobile devices, and because the link is external, download attribute didn’t work. I came up with the idea I created download method act as a relay. When I run project on local, it works. I deployed the project to the VPS using tomcat and using nginx reverse proxy. Project works, but site is not accessible in the same profile of browser (it works on incognito or different browser etc. Also I’m able to open new tab in Chrome on IOS and download another video) until download finishes. I also dockerized the app, but still same thing happens. Not just this project subdomain, but whole domain and subdomains didn’t work. Here’s the download method
@GetMapping("/download")
public void downloadVideo(@RequestParam("url") String videoUrl, HttpServletResponse response) throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet(videoUrl);
try (ClassicHttpResponse httpResponse = client.executeOpen(null, request, null)) {
int statusCode = httpResponse.getCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
String fileName = extractFileName();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="" + fileName + """);
response.setHeader("Content-Length", String.valueOf(entity.getContentLength()));
try (InputStream inputStream = entity.getContent();
OutputStream outputStream = response.getOutputStream()) {
byte[] buffer = new byte[8192]; // 8KB buffer
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
} finally {
EntityUtils.consume(entity); // Ensure the entity is fully consumed
}
}
} else {
throw new IOException("Failed to download video. Status code: " + statusCode);
}
}
}
}
Here’s the html that show content
<a class="btn btn-primary download-button" target="_blank"
th:href="@{~/download(url=${video.videoPath})}" th:download="${video.title}+.mp4">Download video</a>
And there’s nginx reverse proxy conf
#PROXY-START/
location ^~ /
{
proxy_pass http://127.0.0.1:8091;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
# proxy_hide_header Upgrade;
#Persistent connection related configuration
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* ".(gif|png|jpg|css|js|woff|woff2)$" )
{
expires 1m;
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 1m;
}
#PROXY-END/
I tried to disable cache, buffering etc.