How to upload files to my Box Sub-Folder using either by subfolder name or ID
Example say I have 2 subfolders(subfolder1 and subfolder2) in my Box, How to upload files to subfolder2 using java sdk.
Can we upload using any new methods.
Successful in uploading files to Box root folder using the code below
BoxFolder bfolder = BoxFolder.getRootFolder(api);
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
bfolder.uploadFile(stream, fileName);
You probably need to enumerate the folders till you find subfolder1, then create a new BoxFolder from that. Something like this (edit for compile errors):
BoxFolder bfolder = BoxFolder.getRootFolder(api);
Iterator<BoxFolder.Info> it = bfolder.getChildren().iterator();
for(BoxFolder.Info i : it){
if(i.getName().equals(subfolder1)){
BoxFolder folder = new BoxFolder(api, i.getID());
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
folder.uploadFile(stream, fileName);
break;
}
}
Related
My program was created in Netbeans 8.0.2. The program is supposed to create a (database) folder after installation and extract the contents of a (database) jar file from its library. The folder gets created quite okay, but the contents of the jar file do not get extracted.
How can I get the extraction of the jar file to work?
NB: When I run the program in Netbeans, everything goes well.
Sample Code:
String appHomeDir = new java.io.File(".").getCanonicalPath();
String destDir = appHomeDir + "/database";
File folder = new File(destDir);
if (!folder.exists()) {
folder.mkdir();
String current = new java.io.File(".").getCanonicalPath();
String jarFile = current + "\\app\\lib\\database.jar";
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
if (file.isDirectory()) { // if its a directory, create it
f.mkdir();
continue;
}
java.io.InputStream is = jar.getInputStream(file); // get the input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
while (is.available() > 0) { // write contents of 'is' to 'fos'
fos.write(is.read());
}
fos.close();
is.close();
}
}
So the "database" directory gets created but the contents of "database.jar" do not get extracted.
Problem solved: I replaced "/app/lib/database.jar" with "/lib/database.jar"
if i using HMEFContentsExtractor in POI jar, it can extract html only it is not able extract the image content form winmail.dat(attchment mail sent from oulook), is there any jar file to do this & explain how to use that.
HMEFContentsExtractor ext = new HMEFContentsExtractor(new File(winmailFilename));
File dir = new File(directoryName);
File rtf = new File(dir, "message.rtf");
if(! dir.exists()) {
throw new FileNotFoundException("Output directory "+dir.getName()+" not found");
}
HMEFMessage msg = new HMEFMessage(new FileInputStream(winmailFilename));
msg.getAttachments();ext.extractMessageBody(rtf);
I am trying to load some data saved in my property file in an Android Application.
I have put my property file under the src folder. Every time I try to load data from my file it keeps telling me FileNotFoundException open failed ENOENT (No such file or directory).
My Code is as follows:
This code is to save the file (new create)
File file = new File("src/com/example/testphonegap/SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
This code is to load data
properties = new Properties();
InputStream is = null;
// First try loading from the current directory
try {
File f = new File("src/com/example/testphonegap/SilverAngel.properties");
is = new FileInputStream(f);
// Try loading properties from the file (if found)
properties.load(is);
GetPersonaliseSettings();
GetUserSettings();
GetFavSettings();
}
catch ( Exception e ) {
is = null;
}
Can you tell me what I am doing wrong please? Is it where the file is saved or am I missing something in my code?
It's an Android Application, so the file is stored on the Android device and not on your computer.
If you want to save the file on the SD card you can write the following:
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/subfolderForYourApp");
dir.mkdirs();
This will create a subfolder for your application on the SD card.
To create a new file in this directory:
File file = new File(dir, "SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
Don't forget to add the permission to the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
try this
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/example/testphonegap/SilverAngel.properties");
properties.load(inputStream);
make sure you don't add "src" before property file name.
Put it in the assets folder and load it from there.
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("SilverAngel.properties");
properties.load(inputStream);
I can't seem to import the packages needed or find any online examples of how to extract a .tar.gz file in java.
What makes it worse is I'm using JSP pages and am having trouble importing packages into my project. I'm copying the .jar's into WebContent/WEB-INF/lib/ and then right clicking on the project and selecting import external jar and importing it. Sometimes the packages resolve, other times they don't. Can't seem to get GZIP to import either. The imports in eclipse for jsp aren't intuitive like they are in normal Java code where you can right click a recognized package and select import.
I've tried the Apache commons library, the ice and another one called JTar. Ice has imported, but I can't find any examples of how to use it?
I guess I need to uncompress the gzipped part first, then open it with the tarstream?
Any help is greatly appreciated.
The accepted answer works fine, but I think it is redundant to have a write to file operation.
You could use something like
TarArchiveInputStream tarInput =
new TarArchiveInputStream(new GZipInputStream(new FileInputStream("Your file name")));
TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
while(currentEntry != null) {
File f = currentEntry.getFile();
// TODO write to file as usual
}
Hope this help.
Maven Repo
Ok, i finally figured this out, here is my code in case this helps anyone in the future.
Its written in Java, using the apache commons io and compress librarys.
File dir = new File("directory/of/.tar.gz/files/here");
File listDir[] = dir.listFiles();
if (listDir.length!=0){
for (File i:listDir){
/* Warning! this will try and extract all files in the directory
if other files exist, a for loop needs to go here to check that
the file (i) is an archive file before proceeding */
if (i.isDirectory()){
break;
}
String fileName = i.toString();
String tarFileName = fileName +".tar";
FileInputStream instream= new FileInputStream(fileName);
GZIPInputStream ginstream =new GZIPInputStream(instream);
FileOutputStream outstream = new FileOutputStream(tarFileName);
byte[] buf = new byte[1024];
int len;
while ((len = ginstream.read(buf)) > 0)
{
outstream.write(buf, 0, len);
}
ginstream.close();
outstream.close();
//There should now be tar files in the directory
//extract specific files from tar
TarArchiveInputStream myTarFile=new TarArchiveInputStream(new FileInputStream(tarFileName));
TarArchiveEntry entry = null;
int offset;
FileOutputStream outputFile=null;
//read every single entry in TAR file
while ((entry = myTarFile.getNextTarEntry()) != null) {
//the following two lines remove the .tar.gz extension for the folder name
String fileName = i.getName().substring(0, i.getName().lastIndexOf('.'));
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
File outputDir = new File(i.getParent() + "/" + fileName + "/" + entry.getName());
if(! outputDir.getParentFile().exists()){
outputDir.getParentFile().mkdirs();
}
//if the entry in the tar is a directory, it needs to be created, only files can be extracted
if(entry.isDirectory){
outputDir.mkdirs();
}else{
byte[] content = new byte[(int) entry.getSize()];
offset=0;
myTarFile.read(content, offset, content.length - offset);
outputFile=new FileOutputStream(outputDir);
IOUtils.write(content,outputFile);
outputFile.close();
}
}
//close and delete the tar files, leaving the original .tar.gz and the extracted folders
myTarFile.close();
File tarFile = new File(tarFileName);
tarFile.delete();
}
}
I am trying to have my app export a .csv file that can be picked up in the download APP. The way I am doing this is through fileoutput
I get remotedir By calling Environment.getExternalStorageDirectory().getAbsolutePath()
* Download CSV
*/
public static void downloadCSV(String filename, String remoteDir) throws Exception{
File dir = new File( remoteDir + "/download/");
dir.mkdirs();
//FileWriter f = new FileWriter("Download/" + filename + ".csv");
File fileObject = new File(dir, filename);
fileObject.delete();
fileObject.createNewFile();
ObjectOutputStream objectOut = null;
FileOutputStream stream = new FileOutputStream(fileObject);
objectOut = new ObjectOutputStream(new BufferedOutputStream(stream));
/* Irrelevant code */
String csv = Helper.getCSV(table.getColumnList(), view);
objectOut.writeChars(csv);
objectOut.close();
}
Whenever I test it on my phone(HTC One S) I don't see the file anywhere. I want my csv file to pop up in the Downloads app, but I'm not sure which directory that represents.
Thanks
You might have to scan the file with the MediaScanner before it will show up, at least that's how you'll be able to see it from your desktop for finding/debugging. See the MediaScanner docs for details.