熊星雨的专栏

史上最烂程序员

org.apache.commons.fileupload实现文件上传
<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));
	  }
}


阅读更多
个人分类: java
想对作者说点什么? 我来说一句

org.apache.commons.fileupload

2014年01月02日 143KB 下载

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭