JSP中用Commons-FileUpload组件控制文件的上传

说明:需要添加commons-fileupload-1.2.2.jar和commons-io-2.4.jar

操作的示例代码如下:

<%

  FileItem item = (FileItem)iter,next();

  if(!item.isFormFileld()){

    String filename=item.getName();

    //通过Arrays类的asList()方法创建固定长度的集合

  List<String> fileType = Arrays.asList("gif","bmp","jpg");

  String ext = fileName.substring(fileName.lastIndexOf(".")+1);

  if(!fileType.contains(ext)){

  out.print("上传文件失败,文件类型只能为gif,bmp,jpg");

  }else{

  if(fileName!= null && !fileName.equals("")){

  File fullFile = new File(item.getName());

  File saveFile = new File(uploadFilePath,fullFile.getName());

  item.wrtie(saveFile );

uploadFileName = fullFile.getName();

out.print("上传后的文件名是:"+uploadFileName);

  }

  }

  }

%>

posted @ 2018-01-15 21:24  carrie0608  阅读(578)  评论(0编辑  收藏  举报