I am currently writing a spring boot program that should populate an XLSX template with data and am trying to read the template from inside the project. My directory structure is as follows:
I am now trying to access the file using the following code:
try {
InputStream fis = this.getClass().getResourceAsStream("xlstemplates/billingReview.xlsx");
XSSFWorkbook billingReviewTemplate = new XSSFWorkbook(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Unfortunately I have yet to find a way to read the xlsx file into an InputStream. It always comes back null. Anyone have any pointers on how I can reference that file that is included in the project?
Related
I am trying to get a plain text file from Spring Cloud Config Server using a Spring Boot application. I have put a plain text file example.json in the config git repo in the following structure:
config/appName/schema/example.json
While I am able to access http://localhost:8888/dod-sync/default/master/schema/example.json from the browser, I don't want to just open a HttpConnection and get the file content and parse it in the application.
Is there a way I can load this file somewhat similar to the way to retrieve normal properties in my Spring application?
For example, I can use #Value("${application.http.timeout}") to get the timeout property. I want to have a similar functionality to get the plain text content in the application.
I read https://cloud.spring.io/spring-cloud-config/reference/html/#_serving_plain_text and lots of resources on web but can't find anything helpful.
Thank you for any help!
Is it a property file read case?
try {
InputStream input = new FileInputStream(ConfigJsonTest.class.getResource("/config.properties").getFile());
Properties prop = new Properties();
prop.load(input);
System.out.println(prop.get("timeout"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I created an excel file using POI but now I need to open it using the default .xslx app (MS Excel in my case). I want to specify that I don't need to parse it or read its contents, but just open it in MS Excel.
I figured it out after a while, putting this here in case anyone googles it and this page comes up :
try {
File excelFile = new File(filePath);
Desktop.getDesktop().open(excelFile);
} catch (IOException e) {
e.printStackTrace();
}
I have seen several posts dealing with this error, but none have addressed the specific problem I am encountering.
Here is the code I am testing with.
public class CicTemplateTest {
public static void main(String[] args) {
try {
new CicTemplateTest().process();
System.out.println("DONE xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}
public void process() throws Exception
{
String inputFileName = "Test.xlsx";
String folder = "/Temp3/formaterror/";
File file = new File(folder + inputFileName);
FileInputStream inputStream = new FileInputStream(file);
Workbook wb = new XSSFWorkbook(inputStream); // line where error occurred
OutputStream out2 = new FileOutputStream(new File(folder+"JunkFile.xlsx"));
wb.write(out2);
out2.flush();
out2.close();
}
}
Here is the error I get.
org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:274)
at com.aquent.sdc.jeffk.poiftest.CicTemplateTest.process(CicTemplateTest.java:41)
at com.aquent.sdc.jeffk.poiftest.CicTemplateTest.main(CicTemplateTest.java:25)
The file in question is not an XLS file. It seems to be a weird form of an XLSX file. I wish I could share the file, but I can't as the data is proprietary. I've had to unzip XLSX files in the past and am somewhat familiar with the contents. When I unzip this file the contents do not look normal.
List of files in XLSX file
There are no XML files in there. I've examined every file in the XLSX and they are all binary files. No text file, no XML files. It just doesn't look like a normal XLSX file at all.
It opens fine in Excel, and if I save it, the saved file is a normal XLSX file and sample code works fine.
We get about 1000 users per year submitting these files, and about 15% come to us in this format. I think most of the files with the format problem are from outside of North America.
I have tried reading the file using POI ver 3.9 and 3.14 and get the same error.
I have 2 questions.
How are users creating these files?
How can I get my app to open these files?
My Dataflow pipeline needs to read a resource file GeoLite2-City.mmdb. I added it to my project and ran the pipeline. I confirmed that the project package zip file exists in the staging bucket on GCS.
However, when I try to read the resource file GeoLite-City.mmdb, I get a FileNotFoundException. How can I fix this? This is my code:
String path = myClass.class.getResource("/GeoLite2-City.mmdb").getPath();
File database = new File(path);
try
{
DatabaseReader reader = new DatabaseReader.Builder(database).build(); //<-this line get a FileNotFoundException
}
catch (IOException e)
{
LOG.info(e.toString());
}
My project package zip file is "classes-WOdCPQCHjW-hRNtrfrnZMw.zip"
(it contains class files and GeoLite2-City.mmdb)
The path value is "file:/dataflow/packages/staging/classes-WOdCPQCHjW-hRNtrfrnZMw.zip!/GeoLite2-City.mmdb", however it cannot be opened.
and This is the options.
--runner=BlockingDataflowPipelineRunner
--project=peak-myproject
--stagingLocation=gs://mybucket/staging
--input=gs://mybucket_log/log.68599ca3.gz
The Goal is transform the log file on GCS, and insert the transformed data to BigQuery.
When i ran locally, it was success importing to Bigquery.
i think there is a difference local PC and GCE to get the resource path.
I think the issue might be that DatabaseReader does not support paths to resources located inside a .zip or .jar file.
If that's the case, then your program worked with DirectPipelineRunner not because it's direct, but because the resource was simply located on the local filesystem rather than within the .zip file (as your comment says, the path was C:/Users/Jennie/workspace/DataflowJavaSDK-master/eclipse/starter/target/classes/GeoLite2-City.mmdb, while in the other case it was file:/dataflow/packages/staging/classes-WOdCPQCHjW-hRNtrfrnZMw.zip!/GeoLite2-City.mmdb)
I searched the web for what DatabaseReader class you might be talking about, and seems like it is https://github.com/maxmind/GeoIP2-java/blob/master/src/main/java/com/maxmind/geoip2/DatabaseReader.java .
In that case, there's a good chance that your code will work with the following minor change:
try
{
InputStream stream = myClass.class.getResourceAsStream("/GeoLite2-City.mmdb");
DatabaseReader reader = new DatabaseReader.Builder(stream).build();
}
catch (IOException e)
{
...
}
I am working on a dynamic web project.
On submit button click (present on my form)I want to create a new file and put some data inside.
I have written only these two lines and I am getting failure to create file
try{
File file = new File("C:/database.txt");
file.createNewFile();
}catch(Exception e){
return "error in creating file";
}
If I run the enire code in normal java class everything works fine. Why so?
Your web project is working on application server. The web application can manage files, which are on that server. Other files are not accessible. (of course localhost server is on your computer, but that it's path is not "C:/", so you can't write there). You can find the path of your server running this code (it also create a test file):
String pathWhereYouFindYourFile = new File("").getAbsolutePath();
System.out.println(pathWhereYouFindYourFile);
File f = new File("test.txt");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}