Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Restricting Type of file being uploaded

 
Vikram Virk
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please suggest some ways to restrict type and size of file being uploaded through JSP.I want only image file(gif,jpeg,bitmap..).My action class is

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {

NewsForm newsForm = (NewsForm) form;
FormFile myFile=null;
try{
myFile = newsForm.getTheFile();

String fileName = myFile.getFileName();

InputStream uploadInStream = myFile.getInputStream();

FileOutputStream fOut = new FileOutputStream("d:\\temp\\"
+ fileName);
int c=0;
while ((c=uploadInStream.read()) != -1) {
fOut.write(c);
} // while
fOut.flush();
fOut.close();
}

catch(FileNotFoundException fnfe){
}

Thanks.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!