Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Uploading both local and hosted files  RSS feed

 
Mark JM Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got a simple file upload page in JSP which uploads files to my server posted from the following form:

<form action="upload_code.jsp" method="POST" enctype="multipart/form-data">
<input type="file" name="theFile"><br>
<input type="submit">
</form>

While this code works great for files stored on the local machine, it will not allow you to enter a file which is hosted on-line at an HTTP address.

This is how my upload code starts...


<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();

System.out.println("Content type is " +contentType);

DataInputStream in = new DataInputStream(request.getInputStream());

int formDataLength = request.getContentLength();

.....etc

But the getContentLength line and everything after it fails when trying to pass a file hosted at a HTTP address.

Anyone have any ideas why this is?

Many thanks, in advance.


Mark.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct in assuming that you are trying to type a URL into the file control? What led you to believe that that could work?
 
Mark JM Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right, but it doesn't necessary need to be a file control.

All I want is to create a file uploader that will load a file to my server from either the local file system or from somewhere on the net. So the user types or pastes the address of the file (either file:// or http://) into a text box and then clicks the upload button. Then my upload code does the rest.

So I take it from your response that it's not possible to do it the same way for both types of file? I was hoping the same piece of code would work for both, by using the 'multipart/form-data' method to send the data?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't.
The input file control is very specific in what it does (allow the user to pick a file from the file system).

You would need to pass another parameter to the server with the url of the file that you want to download.
From the server, you could pull that file with either java.net.UrlConnection or with jakarta/commons/fileupload.
http://jakarta.apache.org/commons/fileupload/
 
Mark JM Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, many thanks, I'll give that a try.

Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!