Download a csv file to a shared location, instead of locally, using Spring Boot API - spring-boot

I'm using Spring Boot to download a CSV file (using OpenCV) with a GetMapping request. There is a forced timeout setup on the application server and my request on large files will timeout; 504.
I want to try downloading the file to a shared location and see if that helps (if you have any other suggestions I'm open to new opionions).
my question is, how to hit the API and download the file to a shared location rather than downloading it locally.
here is the controller.
#GetMapping("/download")
public void download(HttpServletResponse r){
string fileName = "xyz.csv";
r.setContentType("text/csv");
r.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
CSVWriter csvWriter = new CSVWriter(r.getWriter());
downloadService.build(csvWriter); //returns csvWriter
}

Related

Problems automatically converting to .txt when downloading xlsx files (excel files) within a Java (SpringBoot) server

When downloading a .xlsx file on a server, it automatically converts to .txt when asked for a save path.
You are currently working with SpringBoot, and if you click the download link through the tag in the view, click the .xlsx file.
The method of loading from the specified path.
The problem is that if you specify the href path for tag a as resource/static within the project, the .xlsx file will be downloaded without any problems.
However, if you route to a folder outside the project, the .xls file is downloaded normally, but. The xlsx file is automatically converted to .txt.
The path imported from the external folder is in the form of a request to the controller to return the actual file.
Below is the code that returns the file from the controller.
I'd like to ask those who know about this problem.
#GetMapping
public byte[] file(HttpServletRequest request) throws Exception {
FileInputStream in = new FileInputStream(new File().getPath() + "/filemanager/"
+ request.getRequestURI().split(request.getContextPath() + "/filemanager/")[1]);
byte[] image = IOUtils.toByteArray(in);
in close();
return image;
}

JMeter: How to access a remote Cloud directory in JMeter?

I need to access a remote cloud directory ( microsoft azure) to list the files in the folder. I also need to move some of the files to another folder in the cloud directory ( cut and paste ).
I found a few answers which spoke about using a Beanshell Sampler and a Foreach controller to get the files in a directory (This was for the folder structure on my local machine). I was able to check the results using a Debug Sampler and a view results tree. However, I am not sure how to use this for a Cloud directory.
I also found answers around using a Directory Listing Config Plugin, this works well with the local directory as well. But I am unable to pass the path to the cloud directory.
Is there a way to access the cloud directory? I am fairly new to JMeter.
Please Help. Thank You.
If you're talking about Azure Files - it can be accessed either via NFS protocol or SMB protocol.
None of the protocols is supported by JMeter or any plugins so you will have to use JSR223 Sampler and write some custom Groovy code using the relevant Java library like EMC NFS Java Client or JCIFS. For the latter one example code can be found in How to Load Test SMB/CIFS with JMeter, example adaptation just in case:
import jcifs.smb.NtlmPasswordAuthentication
import jcifs.smb.SmbFile
String url = "smb://path/to/your/cloud/directory";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "username", "password");
SmbFile smbFile = new SmbFile(url, auth);
SmbFile[] files = smbFile.listFiles("*");
for (int i = 0; i < files.length; i++) {
log.info("File + " + i + ": " + files[i].getName());
}

Update a complete JSON config file from spring cloud config

I am currently setting up a spring cloud config server to deliver some JSON config files from a specific repository, using RabbitMQ to propagate change events to the clients.
Therefore I added a REST endpoint, which delivers all config files, based on a passed branch name:
#RestController
public class RPConfigsEndpoint {
#Autowired
private JGitEnvironmentRepository repository;
private File[] files;
#RequestMapping(value = "/myConfigs")
public File[] getList(#RequestParam(defaultValue = "master") String branch) {
//load/refresh the branch
repository.refresh(branch);
try {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(repository.getBasedir()).readEnvironment().findGitDir().build();
//only return JSON files
files = repo.getDirectory().listFiles((file, s) -> {
return s.toLowerCase().endsWith(".json");
});
} catch (IOException e) {
e.printStackTrace();
}
return files;
}
}
As expected, I get all the files as plain text files... so far so good.
Now my question:
If I modify one of these files and trigger the '/monitor' endpoint on the server, it generates a RefreshEvent as expected:
.c.s.e.MultipleJGitEnvironmentRepository : Fetched for remote master and found 1 updates
o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed []
This event is sent, as the server notices that the commit ID has changed.
The delta of refreshed keys is empty, as it only looks for environment properties (in .yml or .properties files).
Is there a way to reload the whole config file on the client side as it would be done with single properties? Do I therefore need to adapt the change notification?
With my current approach, I would have to reload the whole branch (40 files) instead of reloading only the modified file...
I found a similar question from 2016 on github, without finding the answer.
Thanks in advance for any hint.
Update
repository.findOne(...) and repository.refresh(...) are not thread safe, which must be garanteed in cloud environment, as the service can be contacted by different apps at the same time.
Possible solutions:
'synchronize' the concerned method call(s)
avoid listing up all the files and request single plain text files, as it works out of the box.

How to force download file with Spring controller?

i have a string controller to download a xml file but the download window is open everytime..
this is the code of my controller :
#RequestMapping(value = "/export", method = RequestMethod.POST)
#ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException,
JsonMappingException, IOException, DatatypeConfigurationException {
byte[] bytes = service.exportXML(getUsername());
String xmlFileName = "filename.xml";
response.setContentType("application/force-download");
response.setHeader("Content-Length", String.valueOf(bytes.length));
response.setHeader("Content-Disposition", "attachment; filename="+xmlFileName);
response.getOutputStream().write(bytes);
}
What i can do to browser never open download window and save the file immediately?
Its not possible from server side. Due to securtity reasons browser will not allow to auto download the files if specified in the browser itself.
For example, if you want to auto download and open a docx file type in your browser. Then check this link How can I open these .docx files from Chrome more quickly
Hope you will understand the limitation here.
Try adding this:
response.setHeader("Content-Transfer-Encoding", "binary");
Well i have earlier tried for txt files which opened directly. Nw they get saved instead of opening.

MVC big file download

I am using ASP.net MVC 4.0. I want to download a file after clicking a download button/a link. The problem is that the file is big and I want to show a 'wait image' while downloading. How do i do it? I am getting the file as a stream? Should I use HTPResposneMessage with web-api or FileStreamResult with MVC? The issue is I want to be notified when the download finishes.
My file is being downloaded with a code as below:
public FileStreamResult Download(Guid id)
{
.......
return this.File(cab, "application/octet-stream", id + ".cab");
}
I want to show a spinner in javascript before beginning the download. My code is as follows:
self.IsDownloading(true);
var url = '/Download/' + id;
window.location = url;
self.IsDownloading(false);
But IsDownloading(false) is being executed before downloading the full file. How to make sure that it is done after the full file is downloaded?

Resources