I'm developing a small program that uploads and downloads files from my box account.
I looked at the docs about uploading files and I found this code:
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
FileInputStream stream = new FileInputStream("My File.txt");
rootFolder.uploadFile(stream, "My File.txt");
stream.close();
I don't really understand how it works. Where can I put the path to the file I want to upload? Or should I use different code?
The constructor for FileInputStream takes in a path to a file. The example in the documentation is uploading a file with the path "./My File.txt" relative to the current directory.
To make it a bit clearer, here's an example using a full path:
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
FileInputStream stream = new FileInputStream("/path/to/My File.txt");
rootFolder.uploadFile(stream, "My File.txt");
stream.close();
Related
I'm using Java Docker API and I'm trying to send my text file to the docker container but the file doesn't appear there. I imagine this happens because the file has no title? How can I give the input stream a title?
final String configDir = "C:/teste/configuration.txt";
File file = new File(configDir);
InputStream input = new FileInputStream(file);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(input,"UTF-8");
dockerClient.copyArchiveToContainerCmd("1025c61de603")
.withRemotePath("/tmp/")
.withTarInputStream(tarArchiveInputStream)
.exec();
EDIT: I Don't get any error in my catch. Seems everything works fine but doesn't create. If you know a easy way to send a file to docker container in Java tell me please
Use Files.copy(source, target, REPLACE_EXISTING); to rename files.
I'm trying to create a new file in:
project/src/resources/image.jpg
as follows:
URL url = getClass().getResource("src/image.jpg");
File file = new File(url.getPath());
but I get error:
java.io.FileNotFoundException: file:\D:\project\dist\run560971012\project.jar!\image.jpg (The filename, directory name, or volume label syntax is incorrect)
What I'm I doing wrong?
UPDATE:
I'm trying to create a MultipartFile from it:
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "image/jpeg", IOUtils.toByteArray(input));
You are not passing the image data to the file!! You're trying to write an empty file in the path of the image!!
I would recommend our Apache friends FileUtils library (getting classpath as this answer):
import org.apache.commons.io.FileUtils
URL url = getClass().getResource("src/image.jpg");
final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
FileUtils.copyURLToFile(url, f);
This method downloads the url, and save it to file f.
Your issue is that "image.jpg" is a resource of your project.
As such, it's embedded in the JAR file. you can see it in the exception message :
file:\D:\project\dist\run560971012\project.jar!\image.jpg
You cannot open a file within a JAR file as a regular file.
To read this file, you must use getResourceAsStream (as detailed in this this SO question).
Good luck
I am using iText libraries to create pdf files using java, the file is created and it opens up using adobe, but when I try to read it i get java.io.FileNotFoundException: ErRecord.pdf (The system cannot find the file specified)
FileInputStream input = null;
File file = new File("ErRecord.pdf");
System.out.println(file.canRead());
input = new FileInputStream(file);
file.canRead() returns false, is there a way to read the file or make it readable using iText?
I used getAbsoluteFile() and the path was wrong..
I just used the absolute path
File file = new File("c:/Users/rawan/workspace-luna/Prototype_3/ErRecord.pdf");
and it worked just fine
I am trying to get the following code to work properly. It always prints the output from the catch block, even though the output that is only printed if the file exists, is printed.
String outputFile = "/home/picImg.jpg";
File outFile = new File(outputFile);
if(outFile.exists)
newStatus(" File does indeed exist");
FileOutputStream fos;
try {
fos = new FileOutputStream(outFile);
fos.write(response);
fos.close();
return outputFile;
} catch (FileNotFoundException ex) {
newStatus("Error: Couldn't find local picture!");
return null;
}
In the code response is a byte[] containig a .jpg image from a URL. Overall I am trying to download an image from a URL and save it to the local file system and return the path. I think the issue has to do with read/write permissions within /home/. I chose to write the file there because I'm lazy and didn't want to find the username to find the path /home/USER/Documents. I think I need to do this now.
I notice in the terminal I can do cd ~ and get to /home/USER/. Is there a "path shortcut" I can use within the file name so that I can read/write in a folder that has those permissions?
No. The ~ is expanded by the shell. In Java File.exists() is a method, you can use File.separatorChar and you can get a user's home folder with System property "user.home" like
String outputFile = System.getProperty("user.home") + File.separatorChar
+ "picImg.jpg";
File outFile = new File(outputFile);
if (outFile.exists())
Edit
Also, as #StephenP notes below, you might also use File(File parent, String child) to construct the File
File outFile = new File(System.getProperty("user.home"), "picImg.jpg");
if (outFile.exists())
~ expansion is a function of your shell and means nothing special for the file system. Look for Java System Properties "user.home"
Java provides a System property to get the user home directory: System.getProperty("user.home");.
The advantage of this is, that it works for every operating system that can run the Java virtual machine.
More about System properties: Link.
I am giving image path as ://serverIp/image/xyz.jpg but I am not showing image on my client application.
For this what i have to do?
I am writting my code as: FileInputStream fin = new FileInputStream(path);
I assume you're using Windows network shares? If so, you should use UNC paths. Your path isn't a valid UNC path, so your file will not be found. Also, for reading files from shares, you could use an URI.
Try the following:
File filetoRead = new File(new URI("\\\\serverIp\\image\\xyz.jpg"));
FileInputStream fin = new FileInputStream(fileToRead);
Note that all backslashes are used twice, this is done to escape the backslashes.