web上传下载

需要commons-fileupload.jar 和commons-io.jar

上传:1、接收页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>  
上传文件:
2、处理页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.text.*"%>

<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.FileUpload"%>

<%@ page import="org.apache.commons.fileupload.DefaultFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>



<%@ page import="org.apache.commons.fileupload.FileUploadBase"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>




  
    My JSP 'upload.jsp' starting page



    




  
  
  
    <%
     // 检查输入请求是否为multipart的表单数据。 
     boolean isMultipart = DiskFileUpload.isMultipartContent(request);
     if (!isMultipart) {
      out.print("表单数据格式不是multipart/form-data,或者非法提交");
      return;
     }

     DefaultFileItemFactory factory = new DefaultFileItemFactory();// Create a factory for disk-based file items
     factory.setSizeThreshold(102400);// 设置初始化内存,如果上传的文件超过该大小,将不保存到内存,而且硬盘中(单位:byte)
     File fileTemp = new File("c://temp");// 建立临时目录
     fileTemp.mkdir();
    %>
<%--
  如果上传的文件超过设置内存的大小,将文件保存至该目录,
待文件上传完毕后,自动删除上传的临时文件;
如果多次上传同一个文件,该文件会保存到服务器的临时目录,而不删除
--%>
<%
factory.setRepository(fileTemp);

DiskFileUpload upload = new DiskFileUpload(factory);// 创建文件上传对象
upload.setSizeMax(1024000);// 设置客户端最大上传,-1为无限大(单位:byte)
//upload.setSizeMax(1024000);
//FileUploadBase.SizeLimitExceededException 

List items = null;
try {
  items = upload.parseRequest(request);// 分析request,并将保存结果至List里 
  System.out.println(items);
} catch (SizeLimitExceededException e) {
  out.print("上传的文件超过系统限制(1MB)");
  return;
}

Iterator itr = items.iterator();

while (itr.hasNext()) {// 循环处理表单元素
  FileItem item = (FileItem) itr.next();
  System.out.println("item is: " + item);
  if (item.isFormField()) {// 普通的表单
   if ("name".equals(item.getFieldName())) {
    String name = item.getFieldName();
    System.out.println(name);
    String value = item.getString("utf-8");
    System.out.println(name + ":=" + value);
   }
  } else {// 上传文件
   if (item.getSize() != 0) {
    String filename = item.getName();// 获取文件的客户端上传路径 

  //  System.out.println("最初的filename=" + filename);
    int i = filename.lastIndexOf("\\");
    
	
    filename = filename.substring(i + 1);// 获取文件名
 //   System.out.println("文件名1=:" + filename);
   
      //注意:要支持中文,请用下面这两句,OK,不过如果中文是奇数,好像会出错.
    // System.out.println("文件名:"+new String(filename.getBytes(),"utf-8"));
     filename = new String(filename.getBytes(),"utf-8");
    
      // System.out.println("Realpath is:"+ getServletContext().getRealPath("/"));
    //注意:创建File savedFile时,要在getRealPath("/")的目录下创建一个temp的目录,因为它传了getRealPath("/")+"temp"字符串了.
  ///  File savedFile = new File(getServletContext().getRealPath("/")+ "temp_files");
   // savedFile.mkdirs();

 //File savedFile = new File("D://test//upload3"); 
 	File savedFile = new File("c://QC/upload"); 
	savedFile.mkdirs();

    savedFile = new File(savedFile, filename);

    System.out.println(item);
    item.write(savedFile);
//    out.println("upload success!!!");
out.println("上传成功");
    System.out.println("upload success!!!");
   }
  }
}
%>



  




下载:

1、接收页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
   contentType="text/html; charset=utf-8"%>


 

    
 
>" method="post" enctype="multipart/form-data"> <%=request.getParameter("filename") %>


2、处理页面

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.text.*"%>

<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.FileUpload"%>

<%@ page import="org.apache.commons.fileupload.DefaultFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>



<%@ page import="org.apache.commons.fileupload.FileUploadBase"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>




  
    My JSP 'upload.jsp' starting page



    




  
  
  
 
<%
//下载
	response.reset();  
	String tempPath="C://QC//upload";
	out.print(request.getParameter("filename"));
	String fileName=(request.getParameter("filename"));
	//String fileName="1.sql";
        File file = new File(tempPath, fileName);  
        if (file.exists()) {  
            response.setContentType("utf-8");  
            response.addHeader("Content-Disposition", "attachment; filename=\""  
                    + file.getName() + "\"");//注释1   
            int fileLength = (int) file.length();  
            response.setContentLength(fileLength);  
            if (fileLength != 0) {  
                InputStream in = new FileInputStream(file);  
                byte[] buffer = new byte[4096];  
                ServletOutputStream servletOS = response.getOutputStream();  
                int readLength = -1;  
  
                while ((readLength = in.read(buffer)) != -1) {  
                    servletOS.write(buffer, 0, readLength);  
                }  
  
                in.close();  
                servletOS.flush();  
                servletOS.close();  
            }  
        }  
	

 %>

  





 

 
 

你可能感兴趣的:(web)