In my project i read excel file. this functionality working on local host but i upload this project on server that time i get error like below
java.io.FileNotFoundException: D:/Java_Question.xls (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
Controller.AdminExelSheetController.read(AdminExelSheetController.jav a:190)
Why this error on server?
Read excel file code: Here where changes required ?
FileInputStream inputFile = new FileInputStream("D:/" + fileName);
POIFSFileSystem systemFile = new POIFSFileSystem(inputFile);
HSSFWorkbook wb = new HSSFWorkbook(systemFile);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
if (!"".equals(rows.toString())) {
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
Vector cellStore = new Vector();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
cellStore.addElement(cell);
}rowStore.addElement(cellStore);}
Because your project has a different path on the server.
You should get your file path like this...
String path = ServletContext.getRealPath("/FolderInYourProjectWhereExcelIsLocated");
Related
Hello everyone and thanks for your help in advance. I am trying to use iText to merge all Pdf files contained within a directory. Here is my code:
public class MergeFiles
{
public MergeFiles(string targetDirectory) {
string dest = targetDirectory + #"\Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdfDoc);
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries) {
//PdfMerger merger = new PdfMerger(pdfDoc);
PdfDocument newDoc = new PdfDocument(new PdfReader(fileName));
merger.Merge(newDoc, 1, newDoc.GetNumberOfPages());
newDoc.Close();
};
pdfDoc.Close();
}
}
This code is resulting in the error "System.IO.IOException: The process cannot access the file 'E:\Merged.pdf' because it is being used by another process." however, I am not sure why. Any help would be appreciated.
After these two lines:
string dest = targetDirectory + #"\Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
A new (empty) file with name "Merged.pdf" is created in your target directory, with file stream opened in writing mode to write the result of the merging process.
Then, you are getting the list of file in target directory with string[] fileEntries = Directory.GetFiles(targetDirectory);. This array already includes your newly created Merged.pdf file.
Eventually the code tries to merge the resultant file into itself, which obviously fails.
To avoid this error, either collect the files to merge before creating the target document (but make sure there is no existing "Merged.pdf" file in the target directory already):
string[] fileEntries = Directory.GetFiles(targetDirectory);
string dest = targetDirectory + #"\Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
// The rest of the code
Or, simply remove the target file from fileEntries array manually before merging the files.
I am new to Spring Boot. I have this emailprop.properties in src/main/resource:
//your private key
mail.smtp.dkim.privatekey=classpath:/emailproperties/private.key.der
But I am getting the error as
classpath:\email properties\private.key.der (The filename, directory
name, or volume label syntax is incorrect)
How do I properly load this file?
Update-1
my java code is
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),
emailProps.getProperty("mail.smtp.dkim.privatekey"));
its working as "D:\\WorkShop\\MyDemoProj\\EmailService\\src\\main\\resources\\private.key.der"Instead of emailProps.getProperty("mail.smtp.dkim.privatekey")
Update-2
i have tried java code is
String data = "";
ClassPathResource cpr = new ClassPathResource("private.key.der");
try {
byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
data = new String(bdata, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),data);
Error is : java.io.FileNotFoundException: class path resource [classpath:private.key.der] cannot be resolved to URL because it does not exist
Tried Code is :
ClassPathResource resource = new ClassPathResource(emailProps.getProperty("mail.smtp.dkim.privatekey"));
File file = resource.getFile();
String absolutePath = file.getAbsolutePath();
Still same error..
please update the answer..
If you want to load this file runtime then you need to use ResourceLoader please have a look here for the documentation - section 8.4.
Resource resource = resourceLoader.getResource("classpath:/emailproperties/private.key.der");
Now if you want to keep this exact path in properties file you can keep it there and then load it in your Autowired constructor/field like that:
#Value("${mail.smtp.dkim.privatekey}") String pathToPrivateKey
and then pass this to the resource loader.
Full example you can find here. I don't want to copy paste it.
If your file is located here:
"D:\\WorkShop\\MyDemoProj\\EmailService\\src\\main\\resources\\private.key.der"
then it should be:
mail.smtp.dkim.privatekey=classpath:private.key.der
EDIT:
I see now, you are using DKIMSigner, which expects file-path string,
Try changing your code like this:
ClassPathResource resource = new ClassPathResource(emailProps.getProperty("mail.smtp.dkim.privatekey"));
File file = resource.getFile();
String absolutePath = file.getAbsolutePath();
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),absolutePath
);
I am Creating a new File using RandomAccessFile with "rw" mode. but it gives
java.io.FileNotFoundException: ../dir/test.txt (No such file or directory)
reference
This is how i created:
File baseDirAsFile = new File("../");
File dirFile = new File(baseDirAsFile, "dir");
File file = new File(dirFile, "test.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
Note:
It is not throwing that exception all the time. But can't identify when and why it is throwing this at some particular time.
I believe your code should be:
String baseDir = new File(".").getAbsolutePath();
String dirFile = baseDirAsFile + File.separator + "dir";
File file = new File(dirFile + File.separator + "test.txt");
RandomAccessFile file = new RandomAccessFile(file, "rw");
I try to save files in IsoStore. In WP8 emulator files have been successfully saved, but when I run my program in other emulators or on my phone(with WP7.8) I get a error: "path must be a valid file name"
I do this:
var path = #"\Shared\Media\mapp\";
var imageName = guid from the server;
if (!_fileStorage.DirectoryExists(path))
_fileStorage.CreateDirectory(path);
//here I get a error using (IsolatedStorageFileStream fileStream =
_fileStorage.OpenFile(path + imageName,
FileMode.OpenOrCreate))
{//do anything}
I try to set path = #"iso:\Shared\Media\mapp\" or #"isostore:\Shared\Media\mapp\" or #"files:\Shared\Media\mapp\" or #"file:\Shared\Media\mapp\" and it doesn't work.
If I set #"\Shared\Media\" all fine in all devices. Who can tell me why I can't create a directory?
For Windows-Phone-7 you can't create a directory, which name ends with "/" or "//", that will cause an "path must be a valid file name" error.
To solve your problem, just change your code a bit:
var path = #"\Shared\Media\mapp";
var imageName = guid from the server;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.FileExists(path))
{
store.CreateDirectory(path);
}
store.OpenFile(path + "\\" + imageName, FileMode.OpenOrCreate);
}
Hope, that helps.
When the first time my app starts on a windows phone, I want to get some files(xml/images) from project folders and write them to the isolated storage .
How do I detect that my app is running for the first time?
How do I access file in project folders?
Here is another way to read files from your visual studio project. The following shows how to read a txt file but can be used for other file as well. Here the file is in the same directory as the .xaml.cs file.
var res = App.GetResourceStream(new Uri("test.txt", UriKind.Relative));
var txt = new StreamReader(res.Stream).ReadToEnd();
make sure your file is marked as Content.
If you mean project folders as in the folders in your visual studio project, I usually right click on the files and set the build action to 'Embedded Resource'. At runtime, you can read the data from the embedded resource like so:
// The resource name will correspond to the namespace and path in the file system.
// Have a look at the resources collection in the debugger to figure out the name.
string resourcePath = "assembly namespace" + "path inside project";
Assembly assembly = Assembly.GetExecutingAssembly();
string[] resources = assembly .GetManifestResourceNames();
List<string> files = new List<string>();
if (resource.StartsWith(resourcePath))
{
StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(resource), Encoding.Default);
files.Add(reader.ReadToEnd());
}
To read the images you would need something like this to read the stream:
public static byte[] ReadAllBytes(Stream input)
{
byte[] buffer = new byte[32 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}