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();
}
Related
I am looking to get the file path of a folder and then get its content. The folders are a public folder that you can access by just connecting the device into the computer. How do I go about getting a the file path to get access to these public folders and then get all its content?
So once I connect my computer to the device these are the folders I see and there is one that says "Folder to get items from" and I can't seem to figure what the file path is in order to get access to its content .
This is how I'm trying to get its content and Log them
File fileOfEpubs = new File("/data/data/Folder of to get items from/");
File[] dirEpub = fileOfEpubs.listFiles();
if (dirEpub.length != 0){
for (int i = 0; i < dirEpub.length; i++){
String fileName = dirEpub[i].toString();
Log.i("File", "File name = " + fileName);
}
}
You appear to be looking at the root of external storage. Programmatically, you access that via Environment.getExternalStorageDirectory(). That requires either the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission.
You may need to escape the spaces in your string
File fileOfEpubs = new File("/data/data/Folder\ of\ to\ get\ items\ from/");
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 am trying to explore Apache commons configuration to dynamically load the property file and do modification in the file and save it.
I wrote a demo code for the same.
Code Snippet
package ABC;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
public class Prop {
public static void main(String[] args)
{
try {
URL propertiesURL = Prop.class.getResource("/d1.properties");
if (propertiesURL == null) {
System.out.println("null");
}
String absolutePath=propertiesURL.getPath();
PropertiesConfiguration pc = new PropertiesConfiguration(absolutePath);
pc.setReloadingStrategy(new FileChangedReloadingStrategy());
String s=(String)pc.getProperty("key_account_sales");
System.out.println("s is " + s);
pc.setAutoSave(true);
pc.setProperty("key_account_sales", "Dummy");
pc.save();
System.out.println("Modified as well");
String sa=(String)pc.getProperty("key_account_sales");
System.out.println("s is " + sa);
}catch(ConfigurationException ce)
{
ce.printStackTrace();
}
}
}
Although when I run the code multiple times, the updated value for the property is being properly shown but the changes are not seen in the Property file.
I tried refreshing the entire workspace and the project but still the property file shows the previous entry whereas this code displays the updated entry in console.
Why my property file is not getting updated?
Well I noticed that a new file with same name was formed inside bin
directory of my IDE workspace. This new file contains the required
changes.
However I still want that the old file should be updated with the new
value and instead of creating a new file, it should update in the old
file itself.
My property file is located inside a Web Application package say
Dem1
by the name of
Prop1.prop
I want to read this property file from in another class say
Reading.java
located inside another package
Dem2
, do changes in this same property file and show it to another user. It is a web application being deployed on an application server.
Even after using the absolute path in a simple file (main function) it is not reflecting the changes in the same file but updating it in new file.
I am doing a very slight mistake but can someone please help.
Using absolute path I am not able to make changes in the same property file in normal main method also. Please suggest.
New file in bin directory is created instead of updating the same file
in src folder.
You should be able to solve this using absolute paths. The PropertiesConfiguration class is finding your properties file somewhere on the classpath and only knows to write back to "d1.properties"; hence you have a file appearing in your bin directory.
The absolute path can be obtained by querying resources on the classpath. Something like the following:
URL propertiesURL = Prop.class.getResource("/d1.properties");
if (propertiesURL == null) {
// uh-oh...
}
String absolutePath = propertiesURL.getPath();
// Now use absolutePath
I am currently working on an application, where users are given an option to browse and upload excel file, I am badly stuck to get the absolute path of the file being browsed. As location could be anything (Windows/Linux).
import org.apache.myfaces.custom.fileupload.UploadedFile;
-----
-----
private UploadedFile inpFile;
-----
getters and setters
public UploadedFile getInpFile() {
return inpFile;
}
#Override
public void setInpFile(final UploadedFile inpFile) {
this.inpFile = inpFile;
}
we are using jsf 2.0 for UI development and Tomahawk library for browse button.
Sample code for browse button
t:inputFileUpload id="file" value="#{sampleInterface.inpFile}"
valueChangeListener="#{sampleInterface.inpFile}" />
Sample code for upload button
<t:commandButton action="#{sampleInterface.readExcelFile}" id="upload" value="upload"></t:commandButton>
Logic here
Browse button -> user will select the file by browsing the location
Upload button -> on Clicking upload button, it will trigger a method readExcelFile in SampleInterface.
SampleInterface Implementation File
public void readExcelFile() throws IOException {
System.out.println("File name: " + inpFile.getName());
String prefix = FilenameUtils.getBaseName(inpFile.getName());
String suffix = FilenameUtils.getExtension(inpFile.getName());
...rest of the code
......
}
File name : abc.xls
prefix : abc
suffix: xls
Please help me in getting the full path ( as in c:.....) of the file being browsed, this absolute path would then be passed to excelapachepoi class where it will get parsed and contents would be displayed/stored in ArrayList.
Why do you need the absolute file path? What can you do with this information? Creating a File? Sorry no, that is absolutely not possible if the webserver runs at a physically different machine than the webbrowser. Think once again about it. Even more, a proper webbrowser doesn't send information about the absolute file path back.
You just need to create the File based on the uploaded file's content which the client has already sent.
String prefix = FilenameUtils.getBaseName(inpFile.getName());
String suffix = FilenameUtils.getExtension(inpFile.getName());
File file = File.createTempFile(prefix + "-", "." + suffix, "/path/to/uploads");
InputStream input = inpFile.getInputStream();
OutputStream output = new FileOutputStream(file);
try {
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
// Now you can use File.
See also:
How to get the file path from HTML input form in Firefox 3
I remember to have some problem with this in the past too. If I am not mistaken, I think you cannot get the full file path when uploading a file. I think the browser won't tell you it for security purposes.
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.