欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSP使用commons-fileupload实现文件上传实例 博客分类: JavaWeb jSP IO流 

程序员文章站 2024-02-25 23:43:45
...
之前用smartupload上传文件,发现处理较大文件时会内存溢出,在网上了解到可修改系统设置来解决此问题:
(1). 在windows 更改系统环境变量
加上JAVA_OPTS=-Xms64m -Xmx512m
(2). 修改TOMCAT_HOME/bin/catalina.sh
在“echo "Using CATALINA_BASE:   $CATALINA_BASE"”上面加入以下行:
JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m"
但还是不太好使,后来改成了commons-fileupload
供大家参考,共同学习!

以下是commons-fileupload实现JSP上传文件的主要代码:
<%@ page language="java"  pageEncoding="GBK"%>

<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.io.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileUploadException" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>上传文件操作</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <%		

try{

	String fileType = request.getParameter("fileType"); //获取页面提交的文件	指定文件夹
	String fileName = request.getParameter("fileName"); //文件名
	fileName=new String(fileName.getBytes("ISO-8859-1"),"GBK");	  
	String  fileNameStr = new String(fileName.getBytes("GBK"), "ISO-8859-1");  
	//System.out.println("fileName"+fileName);
	//System.out.println("fileNameStr"+fileNameStr);
          
	boolean isMultipart = ServletFileUpload.isMultipartContent(request);//检查表单中是否包含文件
	if(isMultipart){
  		FileItemFactory factory = new DiskFileItemFactory();
   		//factory.setSizeThreshold(yourMaxMemorySize); 此处可设置使用的内存最大值
   		//factory.setRepository(yourTempDirectory); 文件临时目录

   		ServletFileUpload upload = new ServletFileUpload(factory);
		long maxsize = 80*1024*1024;
   		upload.setSizeMax(maxsize);//允许的最大文件尺寸		
		java.util.List itemss = new ArrayList();
   		itemss = upload.parseRequest(request);
		String u_name = "E:/founder_erp_upload/"+ fileType + "/" + fileName;//保存到文件目录
		//System.out.println("===================1======================="+itemss.size());
   		for(int i=0;i<itemss.size();i++){
   			FileItem fi = (FileItem)itemss.get(i);
			//fi.write(new File(u_name));
			
   			if(fi.isFormField()){//如果是表单字段
				//System.out.println("==================2=======================");
   			}else{//如果是文件
   				DiskFileItem dfi = (DiskFileItem)fi;
   				//if(!dfi.getName().trim().equals("")){
					//getName()返回文件名称,如果是空字符串,说明没有选择文件。	
   					dfi.write(new File(u_name));
					//System.out.println("==================3=======================");
   				//}
   			}
   		}
	}             

         // 另存到操作系统的根目录为文件根目录的目录下
         //file.saveAs("E://upload//"+ fileType + "//" + fileName +"." + file.getFileExt() ,su.SAVE_PHYSICAL);	
	}catch(Exception e){
		e.printStackTrace();		
	}finally {
	}	
%>        
<script type="text/javascript">
 	window.alert("上传成功!");
 	window.close();
 </script>
  </body>
</html>

相关标签: jSP IO流