i've been trying to download multiple files as a zip from the database using Spring Boot.
I am using Mysql and i did store the file succesfully on the database, but i can t find any help on how to retrive a list of files put it in a zip and download it.
I did manage to get files paths from database and download a zip file , but i would prefer getting some help in how to get the files directly from the database.
This is the code i am using to get a zip file out of local files.
WebService:
#GetMapping("/downloadZipFile/demande/{demandeRef}")
public void downloadZipFile(HttpServletResponse response,#PathVariable String demandeRef) throws IOException {
List<String> listOfFileNames = new ArrayList<>();
Demande d = demandeService.findByReference(demandeRef);
for (DemandePieceJointe dmd : d.getDemandePieceJointes() ){
listOfFileNames.add(dmd.getPath());
}
demandePieceJointeService.downloadZipFile(response, listOfFileNames);
}
Service:
#Override
public void downloadZipFile(HttpServletResponse response, List<String> name) throws IOException {
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=download.zip");
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
for (String fileName : name) {
File resource = new File(fileName);
FileInputStream fileInputStream = new FileInputStream(resource);
ZipEntry zipEntry = new ZipEntry(resource.getName());
zipEntry.setSize(resource.getTotalSpace());
zipOut.putNextEntry(zipEntry);
StreamUtils.copy(fileInputStream, zipOut);
zipOut.closeEntry();
}
zipOut.finish();
zipOut.close();
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"\"");
Related
I want to make a download functionality which has to download a file from the server using spring boot.
The size limit of the file should be 1gb and I should have an option to download it as a zip file
Hello to download any type of file you can use the following method. You must have configured the path before applying it.
//descargar archivo
#Secured({ "ROLE_ADMIN", "ROLE_USER","ROLE_SADMIN","ROLE_USERGESTSERV"})
#RequestMapping(value="/downloadFile/{filename:.+}")
public void getLogFile(HttpSession session,HttpServletResponse response,#PathVariable String filename) throws Exception {
try {
final Logger log = LoggerFactory.getLogger(getClass());
Path pathFoto = getPath(filename);
log.info("pathFoto: " + pathFoto);
Resource recurso = new UrlResource(pathFoto.toUri());
File fileToDownload = new File(pathFoto.toString());
InputStream inputStream = new FileInputStream(fileToDownload);
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;recurso");
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
inputStream.close();
} catch (Exception exception){
System.out.println(exception.getMessage());
}
}
I use Apache Solr so that I can work with files, I can add regular text fields via Spring, but I don’t know how to add TXT / pdf
#SolrDocument(solrCoreName = "accounting")
public class Accounting {
#Id
#Field
private String id;
#Field
private File txtFile;
#Field
private String docType;
#Field
private String docTitle;
public Accounting() {
}
public Accounting(String id, String docType, String docTitle) {
this.id = id;
this.docTitle = docTitle;
this.docType = docType;
}
here is the problem with the txtFile field
<field name="docTitle" type="strings"/>
<field name="docType" type="strings"/>
These fields that I manually added to schema.xml, I can not figure out how to add a field here that will be responsible for the file, for example, I will add here a txt file, how to do it? Thank you very much. And do I correctly declare the field private File txtFile; in the entity for the file?
Solr will not store the actual file anywhere. Depending on your config it can store the binary content though. Using the extract request handler Apache Solr which relies on Apache Tika to extract the content from the document.
You can try something like below code. The current code is not using anything from the springboot. Here the content is read from the pdf document and then the data is indexed into solr along with id and filename. I have used the tika apis to extract the content of the pdf.
public static void main(final String[] args) throws IOException, TikaException, SAXException {
String urlString = "http://localhost:8983/solr/TestCore1";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
File file = new File("C://Users//abhijitb//Desktop//TestDocument.pdf");
FileInputStream inputstream = new FileInputStream(file);
ParseContext pcontext = new ParseContext();
// parsing the document using PDF parser
PDFParser pdfparser = new PDFParser();
pdfparser.parse(inputstream, handler, metadata, pcontext);
// getting the content of the document
//System.out.println("Contents of the PDF :" + handler.toString());
try {
String fileName = file.getName();
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "123456");
document.addField("title", fileName);
document.addField("text", handler.toString());
solr.add(document);
solr.commit();
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}
Once you index the data, it can be verified on the solr admin page by querying for it.
Please find the image for your reference.
I want to load multiple CSV files into mysql database at single table using
Spring Batch. The path of the files are derived from the following method.
public List<String> getFilePath() {
String inputPath = "E:\\input";
List<String> inputCSVPaths = new ArrayList<String>();
Map<String, List<String>> inputInfo = new HashMap<String, List<String>>();
File inputFolder = new File(inputPath);
File[] inputFiles = inputFolder.listFiles();
for (File file : inputFiles) {
inputCSVPaths.add(file.getAbsolutePath());
}
inputInfo.put("Introduction", inputCSVPaths);
List<String> inputFile = inputInfo.get("Introduction");
System.out.println("Input File :"+inputFile);
return inputFile;
}
There are total 3 CSV files. But it reads only onle file and inserts data of only that CSV file. Is there wrong in getting resources.
#Autowired
private FilePathDemo filePathDemo;
#Bean
public MultiResourceItemReader<Introduction> multiResourceItemReader() throws IOException {
MultiResourceItemReader<Introduction> multiReader = new MultiResourceItemReader<Introduction>();
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources;
String filePath = "file:";
List<String> path = filePathDemo.getFilePath();
for (String introPath : path) {
System.out.println("File Path of the Introduction CSV :" + introPath);
resources = patternResolver.getResources(filePath + introPath);
multiReader.setResources(resources);
}
FlatFileItemReader<Introduction> flatReader = new FlatFileItemReader<Introduction>();
multiReader.setDelegate(flatReader);
flatReader.setLinesToSkip(1);
flatReader.setLineMapper(new DefaultLineMapper<Introduction>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(new String[] { "id", "name", "age", "phoneNo"});
}
});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Introduction>() {
{
setTargetType(Introduction.class);
}
});
}
});
flatReader.close();
multiReader.close();
return multiReader;
}
There are two issues with your configuration:
You are reassigning the resources array with a single file in the for loop. Hence, the MultiResourceItemReader will be configured with only one file.
You are calling the close method on the MultiResourceItemReader and the delegate FlatFileItemReader but you should not. Spring Batch will call those methods when the step is complete.
You can find an example of how to configure the MultiResourceItemReader here: https://docs.spring.io/spring-batch/4.0.x/reference/html/readersAndWriters.html#multiFileInput
I have built a Spring boot MVC application with a Tree data structure in place of an actual database. The program reads from a text file and stores words in the tree. originally I used a the CommandLineRunner class to populate the tree, which works... but after creating a fat jar and running the jar, I get a file not found exception. how can I build a fat jar with maven that includes the text file with maven?
the file is currently in the project root.
here is the logic to generate the tree:
#Component
#Order(value = Ordered.HIGHEST_PRECEDENCE)
public class GenerateTree implements CommandLineRunner {
#Autowired
TreeRepository trie = new TreeRepository();
#Autowired
FileReader fileReader = new FileReader();
#Override
public void run(String... args) throws Exception {
for (String s : fileReader.readFile("wordList1.txt")){
trie.add(s);
}
}
}
here is the logic that reads in the file:
#Component
public class FileReader {
List<String> readFile(String filename){
List<String> list = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(filename))) {
list = stream
.filter(line -> line.matches("[a-zA-Z]+"))
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
You cannot access a File inside a jar (see https://stackoverflow.com/a/8258308/4516887).
Put the wordlist.txt into the src/main/resources directory and read its contents using a [ClassPathResource][1]:
ClassPathResource resource = new ClassPathResource("worldlist.txt");
try (InputStream in = resource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
String line;
while((line = reader.readLine()) != null) {
...
}
}
I can download data via HttpUrlConnection and InputStream but I need to download raw-data. So, i want to create a DownloadManager via raw-data, then using raw-data I convert this data to binary or image format. According to my research, I see "download file from url" but I can't download file in mac? Always, I get FileNotFoundException. Please help me. How I can download data from url?
public class DownloadData extends AsyncTask<Void,Void,Void> {
#Override
protected Void doInBackground(Void... params) {
try {
downloadData("https://blablalabla/get");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void downloadData(String myurl) throws IOException {
URL u = new URL(myurl);
InputStream is = u.openStream();
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[1024];
int length;
OutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/Users/ilknurpc/Desktop/text.docx"));
while ((length = dis.read(buffer))>0) {
fos.write(buffer, 0, length);
}
}
}
If you want to construct a workable download manager, I would suggest that you take a look at the
Tomcat Default Servlet Implementation
.
There a few number of HTTP headers that you need to understand such as E-Tags and Http Range Headers for a proper implementation.
Thankfully the Tomcat Default Servlet handles the prerequisites for you.
You can adapt this servlet in your code with minor changes (package declaration etc).