Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload files... 4

Status
Not open for further replies.

tarawfp

IS-IT--Management
Jul 5, 2004
17
0
0
US

I want to creat a web page that allows me to upload file and generate a dynamic link by using the file name...any hints on how to do that

Many Thanks!!
 
JavaScript and HTML.

Using the HTML:

<input type='file' ...>

...will get you the upload-ability. When the user has finished using the 'Browse...' button to find and select a file, the input VALUE attribute will be the path of that file.

You can then use JavaScript to set the destination of a link on the page to that path (if, for example, you want to allow the user to preview the chosen file).

That's not a complete explanation, but does that sound along the lines of what you're asking for?

--Dave
 

Thanks Dave, this is what I want to do but can you give me some more code info. about how to do it or direct me to a site that has such information...I need to use both jsp for presentation and servlets for manipulations. and yes I want the user to view the uploaded file. a link leading to the uploaded file should remain on the web page and that file can be viewed by others.

Waiting for your feedback
 
Uploading is a tricky beast (from what I can tell).

You need this in your HTML:

Code:
<form name="whatever" method=POST action="yourUpload.jsp" enctype="multipart/form-data">

and an INPUT tag:

Code:
<input type='file' name='fileName' size='30' />

Now, what to put in your yourUpload.jsp (the action destination in the FORM tag, above) is dependent upon a lot of things and I'm not sure I can help you there. If I have to do this here, I copy other code we have.

Anyone else?

--Dave
 
I'm looking into this at the moment [uploadsing images] and it is indeed a tricky beast - how do you get the binary image from the request object? I've seen comments that you need to write a custom HttpServletRequest, but I'm sure there's some code on the web somewhere to do it. There is some shareware [not for commercial use] that I've seen too.

Greg.
 
As with many things, its not tricky if you know how !
You just need to read the input stream correctly, you don't need any custom objects.

upload prompt html page :
Code:
<FORM ACTION="upload.jsp" ENCTYPE="multipart/form-data" METHOD="post" >
	<HR>
	<INPUT TYPE="file" NAME="fileToUpload"> <BR>

	<INPUT TYPE="submit" VALUE="UpLoad">
</FORM>

and the JSP (I would actually put this in a servlet, but as an example) to upload the file and save it on the server :

Code:
<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
	String contentType = request.getContentType();
	System.out.println("Content type is :: " +contentType);
	if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
		DataInputStream in = new DataInputStream(request.getInputStream());
		int formDataLength = request.getContentLength();

		byte dataBytes[] = new byte[formDataLength];
		int byteRead = 0;
		int totalBytesRead = 0;
		while (totalBytesRead < formDataLength) {
			byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
			totalBytesRead += byteRead;
		}

		String file = new String(dataBytes);
		String saveFile = file.substring(file.indexOf("filename=\"") + 10);
		saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
		saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

		int lastIndex = contentType.lastIndexOf("=");
		String boundary = contentType.substring(lastIndex + 1,contentType.length());

		int pos;
		pos = file.indexOf("filename=\"");
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;

		int boundaryLocation = file.indexOf(boundary, pos) - 4;
		int startPos = ((file.substring(0, pos)).getBytes()).length;
		int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

		FileOutputStream fileOut = new FileOutputStream(saveFile);
		//fileOut.write(dataBytes);
		fileOut.write(dataBytes, startPos, (endPos - startPos));
		fileOut.flush();
		fileOut.close();
		
		out.println("File saved as " +saveFile);
	}
%>

--------------------------------------------------
Free Database Connection Pooling Software
 
sedj, in your code, where is the file being saved at on the server?
 
In sedj's code, creating the FileOutputStream object effectively creates the file on the server. The subsequent writing to the file is writing to the server. No additional saving is required.

--Dave
 
sorry, I meant where it's being created on the server?
 

The file is saved in the bin directory of the server. Im still looking into adding a link to the file...any ideas
 
Ah...

In his code, the variable saveFile is not just the file name, but also the path. Control that value and you control where the file goes.

--Dave
 
oh I see, I ran it as an example on windows on tomcat with mysql "as is" and it put it in c:/system32 lol i understand now, Thank You
 
so this code:

Code:
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

will put the file you uploaded to C:\WINDOWS\SYSTEM32 for example if you uploaded to a server running windows. however, I'm having syntax problems trying to specify the path I want it saved to. anybody know of an example if i was trying to specify a path on a linux server?
 
The example I originally gave was meant to show you how to upload a file to a server, I was kinda guessing you knew Java ...


This is how you write a file (as the example shows), try to modify the examples to get what you wish for ...
Code:
File f = new File("/my/path/to/god/knows/where");
FileOutputStream fos = new FileOutputStream(f);
fos.write("Hello, my name is whatever".getBytes());
fos.flush();
fos.close();

--------------------------------------------------
Free Database Connection Pooling Software
 
Oops, my bad. I should have noticed the absence of the FILE object. sedj, you did show a STRING object as the parameter in the constructor of the FILEOUTPUTSTREAM in your original code.

Here's some code from a file of mine (it's pretty much the same as sedj's last contribution):

Code:
String filePathName = "e:\\iishome\\httpd\\"+fileName;
File myFile = new File(filePathName);

FileWriter newFileWriter = new FileWriter(myFile);
newFileWriter.write(htmlString);
newFileWriter.close();

That was for a FileWriter object. It probably would benefit me to have the .flush() command in there if FileWriter's have one.

'sorry for any confusion.

--Dave
 
I'm stepping back now.

FileOutputStream does have a constructor that takes a STRING argument. Sorry, sedj!

--Dave
 
*bows respectively to sedj*
*bows respectively to Dave*


so the
Code:
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

controls where it goes (in that example it's in the bin directory) so for example:

Code:
saveFile = saveFile.substring(saveFile.lastIndexOf("\path\to\wherever\") + 1,saveFile.indexOf("\""));

is that all I need to edit to control where the file is going to my server? This is all new to me, sorry for being a noob :/
 
sorry, I worded that last one wrong. What I mean is the lastIndoxOf() method, is that the only part I edit to control the path because it gives me a String -1 error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top