• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want to download .doc file from mysql  RSS feed

 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



this code only downloads pdf file if user uploaded, but if user uploaded .doc file,, what else shuld do in the code so, that it is able to download the .doc files too..
Thanks in advance..
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code only downloads pdf file if user uploaded, but if user uploaded .doc file,,

Something is missing in this sentence - a description that tells us, in detail, what happens if this code is used for .doc files.

Obviously, setting the content type to "application/pdf" will not work for file formats other than PDF.
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah appication/pdf mime type allows only pdf file.
and it works fine if user uploads .pdf file...

but if user uploads .doc file,, then i want to download .doc types of files, then what shold i do..
i think i should convert .doc file to .pdf before uploading..
or if it can be done by some changes in the download servlet so that it becomes able to download both mime types..
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if user uploads .doc file,, then i want to download .doc types of files, then what shold i do..

Set an appropriate content type, of course. You can try to infer it from the file extension, or use code other people have already written for this: http://www.rgagnon.com/javadetails/java-0487.html

i think i should convert .doc file to .pdf before uploading..

Why would you want to do that? You just said you *want* to download a DOC.
 
Balaji Vankadaru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
res.setContentType( "application/msword" );

try to set the above content type
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To ulf..

because application/pdf can download only pdf's and on changing mime type to application/doc it will output only doc files
and i want that same servlet is able to download files of both mime types..
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To ulf

sorry , i did not means that,, i don't need to download, but user needs to download or view that doument..

So, User can upload any kind of file, pdf or doc..
and he also want to view his document in the site, so we need to download that file from mysql and show that file in new tab on user screen..

here from above code i am able to show user the pdf file he uploaded, but if he uploads .doc file,, then my servlet code fails to show the .doc file uploaded by the user..
Want solution for the same..

Thanks for trying to solving the issue..
 
Prajakta Acharya
Ranch Hand
Posts: 138
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am thinking if setting MIME content can be done conditionally in the servlet code.

E.g user uploaded some doc and it is saved in your DB. Along with storing the file contents, you can store the file name along with its extension.
While reading the file contents, file name should be read as well and its extension derived.
Accordingly, you would know the MIME type to set. The mapping of extension and content type can be maintaned in a map or so.
What happens when client computer has a setting of 'Hide file extensions for known file types' need to be thought of.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prajakta Acharya wrote:
I am thinking if setting MIME content can be done conditionally in the servlet code.

Yes. The mime type can be set at runtime when the user tries to download the file.

Prajakta Acharya wrote:
Accordingly, you would know the MIME type to set. The mapping of extension and content type can be maintaned in a map or so.

No need to maintain the map. Use built in libraries, which is exactly Ulf's link says.

Prajakta Acharya wrote:
What happens when client computer has a setting of 'Hide file extensions for known file types' need to be thought of.

The hide part is just the view, that too on the client system. It has got nothing to do with the server setting the mime type. Even if the user has this setting enabled (again on the client) the mime type can be resolved from the file name (or better magic number in header)
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah ulf's link is very useful

but, File object is not accepting the path to mysql location..

File f = new File(rs.getString(1));
any solution?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "is not accepting" mean? Please get in the habit to TellTheDetails. We don't have your screen on front of us, so we don't know what's happening.

Note that you can only use absolute paths in web apps - relative paths, or just the file name, don't work.

So: what is the problematic value of "rs.getString(1)" ?
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okeys ulf,

MimetypesFileTypeMap().getContentType(File file)
i.e it take oly file type parameters...

and we need File object

File f = new File(rs.getBlob(1));
ERROR : No suitable constructor found for file(Blob)
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't just randomly mix-and-match the data you have with the methods and classes that you're trying to use; you may need to do some data conversion.

In this case, write the contents of the blob to a temporary file that you can then use. Blob has methods that let you get at the byte[] or an InputStream, both of which you could use for this purpose.

If this was my problem I'd probably use the jmimemagic library, which can work with a byte[], thus making the creation of a temporary file unnecessary.
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ulf,,

File object is not taking byte[] and InputStream

same Error message : No suitable constructor found for file(byte[])
same Error message : No suitable constructor found for file(InputStream)
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re-read the first two paragraphs of my previous post. You do not create a file by passing its data to the File(...) constructor.

Also, you need to get in the habit of reading the javadocs; all the available methods are listed -and generally explained- in them. You can't just make up constructors you'd like to have.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!