I have written a java program which enabled the shared review feature on a pdf. After making the pdf as Shared Review PDF it sends the pdf to a link folder in UNIX machine (link folder means shortcut path of a folder). After that an shell script, which runs differently via crontab makes the pdf as comment enabled and via the script that pdf is copied to some folder.
Now the question is whenever I am copying the file in the UNIX link folder (for copying the file to the link folder I am using FileUtils.copyInputStream() function.), a comment enabled script is executing and doing all comment enabled things in pdf(.sh script in UNIX which runs not from my program.) but during saving the file it prompts the file is readonly and exits without saving the file. But if I do the same thing (putting the file manually to the link folder in UNIX) manually it makes the pdf file as Comment enabled. This is my sample code which sends the file to a folder.
try{
String outFname="SR_"+fname;
srEnabledIs = new FileInputStream(pdfoutputPath+fname);
if(srEnabledIs!=null && !path.equals("")){
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss_a");
String timeStamp=format.format(today);
FileUtils.copyInputStreamToFile(srEnabledIs, new File(path+outFname));
logtracker.writeDebugNormalLog("AnnotationMain","file copied to " + path);
try{
lrfile.renameTo(new File(pdfinputPath+"/pdf/"+fname.split("[.]")[0]+"_"+timeStamp+".pdf"));
}catch(Exception ex){
logtracker.writeDebugNormalLog("AnnotationMain", ex.getMessage());
ex.printStackTrace();
}
}else{
if(path.equals("")){
logtracker.writeDebugNormalLog("AnnotationMain", "File not copied as path not found.");
}
if(srEnabledIs==null){
logtracker.writeDebugNormalLog("AnnotationMain", "File not copied as InputStream is null.");
}
if(srEnabledIs==null && path.equals("")){
logtracker.writeDebugNormalLog("AnnotationMain", "File not copied as InputStream is null and path not found.");
}
}
}catch(IOException e){
logtracker.writeDebugNormalLog("AnnotationMain", e.getMessage());
e.printStackTrace();
}finally{
if(srEnabledIs!=null){
srEnabledIs.close();
}
}
But the same program is running and executing successfully in other UNIX machine. I am really unable to understand the situation. Please help me out of this situation.
Related
In my java code, I am using renameTo method to rename the file. I am able to do it successfully if the file is closed physically. But I am unable to do so if the file I am trying to rename is open.
How do I close the file thru code?
This is the code :
File file = new File("/users/abc.txt");
File newFile = new File("/users/xyz.txt");
if (file.renameTo(newFile)) {
System.out.println("File rename success");
} else {
System.out.println("File rename failed");
}
Thanks in advance.
You cannot close a file in Java that is opened by another user. It is highly platform dependant, it is impossible if that user/process has higher priviledges then your process, and the user editing it might hava data loss, if he is in the process of editing that file in an Editor. Don't ever do that.
The only way would be to wait until the file has been closed by that other user like that, however, I still discourage from doing so. If you have to use that file, just exit your application with an error.
File file = new File("/users/abc.txt");
File newFile = new File("/users/xyz.txt");
try {
while (!file.renameTo(newFile)) {
Thread.sleep(10_000); // wait 10 seconds
}
} catch (InterruptedException ex) {
// ignore
}
I am attempting to overwrite and update an image on a page whenever my end user would like. They would simply upload a new image, and it will replace the old with the same file name and then the src path in the web page does not need to change. However, it kind of works. The file overwrites. but when I refresh the page the image does not change to the new. The odd kicker is, When I go into my IDE (Eclipse) and double-click the new image file, THEN I can refresh the web page and it shows the new replaced one. This is my first job project, and I have not found the answer elsewhere.I will provide the code;
<img th:src="#{/img/uploadedFile.jpg}" alt="image"></img>
#RequestMapping(method=RequestMethod.POST, value="image")
public String processImageForm(#RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:image";
}
String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
Path path = Paths.get(UPLOADED_FOLDER + fileName + extension);
try {
Files.deleteIfExists(path);
} catch (IOException | SecurityException e) {
System.err.println(e);
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Files.write(path, bytes);
redirectAttributes.addFlashAttribute("message", "You successfully uploaded '" + file.getOriginalFilename() + "'");
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:image";
}
}
My guess would be that you are not copying that file to the correct location.
You know that in a web app the the war file is created and then deployed on a server. So in order to change something you need to change it on the server (even when running from Eclipse it creates a server context from you). I guess that's where your problem comes from - you copy the file to the wrong place.
And when you are running from Eclipse the file is properly replaced on your local file system but this doesn't change the deployed war application. When you double click it in Eclipse it notices the change and automatically redeploys it for you basically changing the file on the server.
I am asking of there's a way how I could put like a program or a bat file or any file that has stuff written in it and them when he clicks on a button it will create that file that i have put into my project on to the users desktop is there a way?
File test = new File("C:/Users/"
+ System.getProperty("user.name")
+ "/AppData/Roaming/.minecraft/mods/welcome.txt");
try { test.createNewFile(); }
catch (IOException e1) { e1.printStackTrace(); }
this doesnt work.
If the file you want to creating is already exist in the disk, then you can print a message like the "File already exists" -
try {
File file = new File("c:\\some\location\file_name.txt");
if (file.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
}catch (IOException e) {
e.printStackTrace();
}
In order to just put a file on an other computer you will need to be granted permission in some way to do that (otherwise anyone could just infect any networked computer with any content they desired). There are several ways you could gain access to an other computer (look into virtual private network and mapped network drive). The most common way of delivering files to another computer is to set up a web site where the user can request the file to be downloaded to their machine by clicking on a link. You could also write a client program that used an http get to allow the client user to request content be downloaded to a specific place on their machine.
ok i found out how i just downloaded the files from a server that i have "made"
I am writing a test to check that a file can be downloaded from a particular web page and I want it to be able to run both locally and remotely (i.e. on a node via Selenium grid). Before anyone links me to the 'do you really need to download the file?' article, I have already managed to download and check the file, I just need a way of deleting it after the test has completed. Just calling File.delete(); or similar will only work locally (as far as I'm aware) so I can't use that to delete the file from the node machine. I'm aware of the class org.openqa.selenium.io.TemporaryFileSystem however I can't find any instructions for how to use it.
Can anyone offer a better solution than 'just run a script on the node machine to delete the file'? Thanks!
You can make the download folder shared. \youruser\downloads after that you can pass this path to the File.Delete(); and it will delete the desired files.
Cleanup of downloaded files within session of Grid Node feature is planned:
https://github.com/SeleniumHQ/selenium/issues/11457
https://github.com/SeleniumHQ/selenium/issues/11657
This Worked for me
try
{
if ((new File("Path")).delete()) {
System.out.println("Pass");
} else {
System.out.println("Failed");
}
} catch (Exception ex) {
ex.printStackTrace();
}
----------simply use this code for delete file in any folder-------------------
File file = new File("C:\\Users\\Updoer\\Downloads\\Inspections.pdf");
if(file.delete())
System.out.println("file deleted");
The Below Code will sequentially delete all the files from folder
File path = new File("Path of Folder");
File[] files = path.listFiles();
for (File file : files) {
System.out.println("Deleted filename :"+ file.getName());
file.delete();
}
I have made a Swing application and will include a file, help.pdf in the .jar file. When the user selects Help->User Guide from a JMenuItem, it should load the file in the default PDF viewer on the system.
I have the code to load the PDF,
private void openHelp() {
try {
java.net.URL helpFile = getClass().getClassLoader().getResource("help.pdf");
File pdfFile = new File(helpFile.getPath());
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File does not exist!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
This works in the eclipse IDE, however, when I pack it into a jar for other people it no longer works.
How do I fix this problem?
The problem is that a File cannot name a component of a JAR file. What you need to do is to copy the resource from the JAR file into a temporary file in the filesystem, and open using the File for the temporary file.
File names in a .jar file are case sensitive. In your text you write Help.pdf but in the code you use help.pdf. The upper/lowercase in the Java code must match the case of the file, even if you are using a system where the filesystem is not case sensitive.
Try
getResource("Help.pdf");
instead (assuming the filename in your posting text is correct)
I think you have to retrieve the location of the jar, open it and load the pdf file from within your application. The .jar file is just a zipped archive, which can be read with java easily...