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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to upload??

Status
Not open for further replies.

anuprm

Programmer
Nov 20, 2003
11
IN
Hi,
I am designing an web application where a client has to upload a file in MsWord format.This file should be stored as a BLOB field in a database and not as a file on the server. And any other client should be able to download the file in the same format. I found a way to upload ..but the file contents get converted into plain text..so I want all the formating of the text in the file to remain intact. This is the same way how u upload files in Yahoo or Hot Mail. So if anybody could give me some tips or a link that could help me ..it would be great.And I plan to use JSP and Java Beans.

Thanks,
Bye.
 
On the browser client side, how are you choosing the file, something like this ?

Code:
<FORM ACTION=&quot;/components/servlet/UploadServlet&quot; ENCTYPE=&quot;multipart/form-data&quot; METHOD=&quot;post&quot; >
	<HR>
	<INPUT TYPE=&quot;file&quot; NAME=&quot;fileToUpload&quot;> <BR>

	<INPUT TYPE=&quot;submit&quot; VALUE=&quot;UpLoad&quot;>
</FORM>
 
Yes on the client side I use this and I pass this as a HttpServletRequest object to the JavaBean where the contents are read line by line and converted to String type...but this works fine if the uploaded file is text only, but if the uploaded file has any formating like Bold or Italics then junk characters are stored....so how do I sustain the formatting as well.?
 
Word file itself is in binary form, you have to ensure that data is read and write as bytes, not characters.
 
Have a look at this code, it shows how to read the stream sa bytes from the correct position of the http input stream. You'll have to hack it about a bit, but it shows the general idea :

Code:
HttpSession session = req.getSession(true);
		PrintWriter out = null;
		DataInputStream in = null;
		FileOutputStream fileOut = null;
		String rootPath = (String)session.getAttribute(&quot;fileNamePathToSaveTo&quot;);
		String fileName = null;
		//int MAX_SIZE = 50 * 1024; // 50 Mb
		try {
			resp.setContentType(&quot;text/html&quot;);
			String contentType = req.getContentType();
			System.out.println(&quot;Content type is :: &quot; +contentType);
			//java.util.Enumeration enum = req.getParameterNames();
			//while(enum.hasMoreElements()) {
			//	System.out.println(&quot; param = &quot; + (String)enum.nextElement());
			//}

			if ((contentType != null) && (contentType.indexOf(&quot;multipart/form-data&quot;) >= 0)) {
				in = new DataInputStream(req.getInputStream());
				int formDataLength = req.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(&quot;filename=\&quot;&quot;) + 10);
				saveFile = saveFile.substring(0, saveFile.indexOf(&quot;\n&quot;));
				saveFile = saveFile.substring(saveFile.lastIndexOf(&quot;\\&quot;) + 1,saveFile.indexOf(&quot;\&quot;&quot;));

				int lastIndex = contentType.lastIndexOf(&quot;=&quot;);
				String boundary = contentType.substring(lastIndex + 1,contentType.length());
				fileName = new String(rootPath +saveFile);

				int pos;
				pos = file.indexOf(&quot;filename=\&quot;&quot;);
				pos = file.indexOf(&quot;\n&quot;, pos) + 1;
				pos = file.indexOf(&quot;\n&quot;, pos) + 1;
				pos = file.indexOf(&quot;\n&quot;, 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;

				//File fileDir = new File(rootPath);
				//if (!fileDir.exists()) {
				//	fileDir.mkdirs();
				//}

				fileOut = new FileOutputStream(fileName);
				//fileOut.write(dataBytes);
				fileOut.write(dataBytes, startPos, (endPos - startPos));
				fileOut.flush();
				fileOut.close();

			}
 
Thanks sedj You were a great help.
Cheers,
anup.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top