We are saving .csv files on a tomcat6.0 server that are sent via cron to an external vendor. On occasion, the send doesn't work and we need to get the .csv file from the server and email it to the vendor. Instead of having to log in to the server, I am trying to add another function on our webpage (that lives on the same server) that will allow administrators to download the file to their desktop and email it that way. If I know the name of the file, all is well and I have that part working, but I need to be able to select a file from the directory on the server. Finally my question: How can I show the list of files from a particular directory in my java servlet?
Following function will return an arraylist of files present inside a folder.
public ArrayList<String> getReportNames() throws IllegalArgumentException {
String path=getServletContext().getRealPath("/WEB-INF");
File[] list = new File(path+"/YOUR_FOLDERNAME_INSIDE_WEBINF").listFiles(new MyFileNameFilter());
ArrayList<String> fileNames=new ArrayList<String>();
for (File file: list)
fileNames.add(file.getName());
return fileNames;
}
List the files in the directory (using File.listFiles) and display a page with the list. Unclear if you are asking for something more.
Related
In a folder 5 CSV files and i need to upload one by one for validation purpose but even single file is not uploading i tried so many methods,can anyone suggest any methods, i'm doing in salesforce.
WebElement uploadElement = driver.findElement(By.xpath("//div[#class='cBatchMaster']//input[1]"));
uploadElement.sendKeys("E:\\Automation\\Error Inventory.csv");
above the code is for single file and directly i'm giving the exact file location but i want to upload the files in one folder
Thanks
Below is the solution in C#. A similar approach can be used in Java
Use AutoITX to upload files. You would need to add AutoItX.Dotnet in your nuget package
using AutoIt;
public static void UploadDocument(IWebElement uploadElement, string path)
{
uploadElement.Click();
AutoItX.WinActivate("Open");
string filepath = Path.Combine(System.IO.Path.GetFullPath(#"..\..\"),path);
Thread.Sleep(1000);
AutoItX.Send(filepath);
AutoItX.Send("{ENTER}");
}
You should be able to upload multiple files from AutoIT once you are able to upload the single file
I am super confused on how to make a program that scans a directory for files (and files in sub directories) for information such as size, name, path, etc. For now, I just need this information written to a txt file, but I will later need to use this txt file to copy all my files to a directory on a remote computer.
I’m not very experienced with Java so if there are some libraries that you think I should research, please let me know. I think the scariest part of getting this information is how to tell computer that you need to enter a sub directory to search for its contents (like going into a Documents folder from Home, and then maybe even going into an EnglishClass folder from there).
I hope this isn’t too vague. Let me know if you need more details.
This will print a list of files from the current directory and their sizes:
List<String> fileList = Arrays.asList(new File(".").list());
fileList
.stream() // Use java streaming api.
.map(f -> new File(f)) // Convert file name into file.
.forEach(f -> { // Iterate over each file.
// Print info about each file.
System.out.println("File name:"+f.getAbsolutePath());
System.out.println("File size:"+f.length());
}
);
If you want to scan sub-directories, read up on how to use recursion. (Hint: f.isDirectory() {...} )
With the ID of an item stored on box, you can download it if it's a file, or download its contents if it's a folder. Either way it seems you need to know what sort of thing you're downloading in order to access it, doing either BoxFile file = new BoxFile(api, id); or BoxFolder folder = new BoxFolder(api, id); before handling the actual download.
I was hoping to be able to do something like
BoxItem boxItem = new BoxItem(api, id);
if (boxItem instanceof BoxFile) {
// download file
} else if (boxItem instanceof BoxFolder) {
// download all files in folder
}
sort of like the example in the docs of downloading a folder's contents. However, in that case the ID is that of a specific folder, whereas my ID is for either a folder or a file in the root folder, and I don't want to loop through all of the root folder's contents. And, anyway, BoxItem cannot be instantiated.
How can I tell ahead of time whether I'm downloading a file or a folder, with just the item's ID? If not, is there a way to download the item anyway?
I don't believe the API supports downloading an entire folder (and therefore the SDK doesn't either). The closest you can get is downloading all of the folder's files. That's why BoxFile has a download() method, but BoxFolder and BoxItem don't.
As for checking whether or not an ID corresponds to a file or folder - there isn't a way to tell without trying to make an API request. For example, you could try doing new BoxFile(api, id).getInfo() and seeing if it returns a 404.
I would like to ask if its possible to put text files into my jar, I use them to make my map in my game, but users can get Highscores. now I want to save the Highscores with the map, so I have to save the map on the user their PC. Is there any way how I could do this? I've searched the internet for some ideas but I could not find anything that even came close to what I've wanted. I only had 3/4th of a year java so I don't know much about these things, everything that happens outside the debug of eclipse are problems for me(files are mainly one of those things, null exceptions, etc).
The main question now.
Is it possible to do? If yes, do you have any terms I could search on, or some sites/guides/tutorials? If no, is there any other way how I could save the highscores?
EDIT:
to make clear
Can I get the text file (the text inside the file) to be extracted to a different file in like the home directory of my game (where I save the settings and stuff) the basic maps are inside the jar file, so I want them to be extracted on the first start-up of the program
Greetings Carolien
"extracted to a different file in like the home directory of my game (where i save the settings and stuff) the basic maps are inside the jar file, so i want them to be extracted on the first startup of the program"
You can get the URL by using getClass().getResource()
URL url = getClass().getResource("/res/myfile.txt");
Then create a File object from the URI of the URL
File file = new File(url.toURI());
Then just perform your normal file operations.
if (file.renameTo(new File(System.getProperty("user.home") + "\\" + file.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
Assuming your file structure is like below, it should work fine
ProjectRoot
src
res
myfile.txt
Note: the above is moving the entire file. If you want to extract just the data inside the file, then you can simple use
InputStream is = getClass().getResourceAsStream("/res/myfile.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
The just do normal IO operation with the reader. See here for help with writing the file.
I want to implement dropbox in my java project.
User: If suppose you want to take a printout, instead of carrying a pendrive or sending it to your gmail id, you will just drop that file into on of the folder inside the public folder of the dropbox.
So after reaching the printout shop you will just navigate to the link http://{host}/myfiles. Here it will show the list of file which are there in that perticular folder inside the public dropbox folder, after clicking of a perticular list item it wll download the file, then the user can select a file and give print.
Is there a way to get the file list along with public url in dropbox using Java ?
You can use the createShareableUrl method to get a link for viewing the document. To get the file list, you can try
DbxEntry.WithChildren listing = client.getMetadataWithChildren(root);
The listing is a list of DbxEntry object of the folder. It can be either a file or a folder. For folder you just need do the same thing repeatedly until reach the end.
In Android case, you can create objects DropboxLink for each path in the folder you want, for example, "/Public/", and get their parameter url:
private DropboxAPI<?> dropbox;
...
ArrayList<String> files = new ArrayList<String>();
try {
Entry directory = dropbox.metadata(path, 1000, null, true, null);
for (Entry entry : directory.contents) {
files.add(entry.fileName() + ": "+ files.add(entry.path));
DropboxLink link = dropbox.share(entry.path);
files.add(link.url);
}
} catch (DropboxException e) {
e.printStackTrace();
}