• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to upload file to drop-box with only file name(without path) using java?  RSS feed

 
kamesh vadam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my web application i want to upload file to drop-box. I am getting file name from browser.Is it possible to upload file to drop-box with only file name.

Below the drop-box upload code with java.

File inputFile = new File("New Text Document.txt");
System.out.println("inputFile.getAbsoluteFile(): " + inputFile);
FileInputStream inputStream = new FileInputStream(inputFile);
try {
DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
} finally {
inputStream.close();
}

In the above code the place New Text Document.txt we have to provide total path of file.

please help me.Thanq
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to achieve by that? DropBox is a file system, it makes sense that it needs a full path.

Or are you asking about the very first line of code which reads "inputFile"? If so, web apps have no concept of relative paths, you need to use absolute paths.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kamesh vadam wrote:In my web application i want to upload file to drop-box. I am getting file name from browser.


Then that means that the HTML in the browser has an element which looks like <input type="file">, right? Your server cannot access that file using a Java File object, because the file in question is on a different computer. You need to do a file upload in your web application, which involves accepting the data which will be in the request. That data will be the contents of the file, which is what you want to upload to Dropbox anyway, isn't it?

I suggest Apache FileUpload to do that.
 
kamesh vadam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK i am getting file if i give the same file name in above very first line it is showing file Not Found Exception.
so i should be store this file in temp folder in server right?
then i can give the path that file.like you mean that?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make: the problemis not with uploading to DropBox, it is with upload a file to a servlet container? If so, check out the Apache FileUpload library Paul mentioned, it has a lot of documentation and examples on its web site.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're talking HTTP file upload, there are 2 schemes used by clients.

Internet Explorer passes the FULL client file pathname as part of the submit. This is actually not a good idea, since it tells the server things it doesn't need to know about your local filesystem

Most other clients only pass the filename itself.

To normalize the uploaded name (discarding the client's path), construct a java.io.File object from it, then use the getFilename() method to extract the simple filename without path.

Either way, do NOT store the uploaded data in your WAR. If you want the server to create local files from uploads, write those files to a directory that's external to both your webapp and the webapp server.
 
kamesh vadam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what i have to exactly do......?
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get an input stream to pass to DropBox.
File and FileInputStream are the wrong classes to use in this case. That is for Files already on the server.

You want the input stream from the file that has been uploaded to your servlet/jsp.
As already mentioned the Apache File Upload component will help you do this.


 
kamesh vadam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So which classes i have to use.Please give me the information.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:As already mentioned the Apache File Upload component will help you do this.

The underlined part is a link.
 
kamesh vadam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i was used ServletFileUpload.But the probem is list size(items.size) is 0.But in request object i was getting file.where i have did wrong..?

my code is...


boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
System.out.println("file is available");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
System.out.println("file is available1" + factory.getRepository());
ServletFileUpload upload = new ServletFileUpload(factory);
System.out.println("file is available2" + upload.getSizeMax());
List<FileItem> items = upload.parseRequest(request);
System.out.println("file is available::::" + items.size());
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
System.out.println(item.getName() + ":::::" + item.getFieldName());
}
System.out.println("file is available");
}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't recall how that library works in detail, but make sure you're doing it like the user guide suggests.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i was used ServletFileUpload.But the probem is list size(items.size) is 0.But in request object i was getting file.where i have did wrong..?


Hooray for System.out.println. The best debugging tool known to man ;-)

How are you getting this code to run?
What gets printed out when you run this code?
What proof do you have the request object is getting a file? What tells you this? What tells US this beyond your assertion?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!