关闭

org.apache.commons.fileupload实现文件上传

209人阅读 评论(0) 收藏 举报
分类:
<context-param>
    <param-name>FileUploadToServer</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>FileUploadLocalFolder</param-name>
    <param-value>FILE_UPLOAD</param-value>
  </context-param>
String referenceID = request.getParameter("referenceID");
String toServer = application.getInitParameter("FileUploadToServer");
if(toServer.equals("true")){   
	  String filePath = application.getRealPath("/")+application.getInitParameter("FileUploadLocalFolder");
	  //建立文件工厂
  DiskFileItemFactory factory = new DiskFileItemFactory();
	  //设置上传工厂的限制
  factory.setSizeThreshold(1024 * 1024 * 20);
  //创建一个上传文件的ServletFileUpload对象
  ServletFileUpload upload = new ServletFileUpload(factory);
  List items = upload.parseRequest(request);
	  Iterator<FileItem> iter = list.iterator(); 
  while (iter.hasNext()) {
		    FileItem item = iter.next();
		    String filename = item.getName();		//获取上传文件的名称
		    File outfilepath = new File(filePath);	//创建文件路径
		    if(!outfilepath.isDirectory()){
			      outfilepath.mkdir();
		    }
		    fileItem.write(new File(filePath + "\\" + referenceID + "\\" +filename));
	  }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10260次
    • 积分:165
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条
    文章分类