common-fileupdown上传

1.上传页面

<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*"%>
<%@ page import="com.gree.db.SSABaanCon" %>
<%@ page import="com.gree.db.BaanLogin" %>
<%@ page import="com.gree.base.Tcmcs000" %>
<%@ page import="com.gree.util.*" %>
<%
  /**
   * 散件清单批次
  **/
    Tcmcs000 tcmcs000 = new Tcmcs000();
   	tcmcs000=(Tcmcs000)session.getAttribute("userBean");
   	if(tcmcs000==null){response.sendRedirect("../index.html");return;}
   	
  	String opt="",sql="",comp="",mess="",user="";//一般用
  	opt=request.getParameter("opt");
  	java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  	comp=tcmcs000.getComp();
  	user=tcmcs000.getUsid();  
  
%>
<html>
<head>
<title>散件清单批次</title>
<link href="../css/css.css" rel="stylesheet" type="text/css">
</head>
<body>
  <form name="frm" action="tcibd5129m003.jsp" enctype="multipart/form-data"   method="post">
 	名字1:<input type="text" name="name_gree"/>
 	<br>
 	名字2:<input type="text" name="name_gree"/>
 	<br>
 	文件1:<input type="file" name="myfile1" />
 	<br>
 	文件2:<input type="file" name="myfile2" />
 	<br>
 	<input type="submit" value="提交" />
</form>  
</body>
</html>
 


2、处理页面


<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*"%>
<%@ page import="com.gree.db.SSABaanCon" %>
<%@ page import="com.gree.db.BaanLogin" %>
<%@ page import="com.gree.base.Tcmcs000" %>
<%@ page import="com.gree.util.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="java.io.*"%>


<%
  //String uploadPath="v:\\";
  String uploadPath = "d:\\sjgg";
  File file = new File(uploadPath);
  if(file.exists()){
	  out.print("yes");
  }else{
	  out.print("no");
  }
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  if(isMultipart==true){
	  
      try{
	        FileItemFactory factory = new DiskFileItemFactory();
	        ServletFileUpload upload = new ServletFileUpload(factory);
	        List<FileItem> items = upload.parseRequest(request);//得到所有的文件
			
		Iterator<FileItem> itr0 = items.iterator();
       		while(itr0.hasNext()){//依次表单中的数据
			FileItem item=(FileItem)itr0.next();
	        
	        	if(item.isFormField()){//如果是普通表单项目
	        		
	        		String fieldName = item.getFieldName();  //获取表单中<input type='text'/>的name名称
	        		String fieldVale = item.getString();	 //获取表单中<input type='text'/>的value值
	        		out.write("表单中普通数据名:"+fieldName); out.write("<br>"); 
	        		out.write("表单中普通数据值:"+fieldVale); out.write("<br>"); 
			}
		}

	        Iterator<FileItem> itr = items.iterator();
	        while(itr.hasNext()){//依次处理每个文件
	        	
	         	FileItem item=(FileItem)itr.next();
	        
	        	if(!item.isFormField()){
	        		
	        		String fileName=item.getName();//获得文件名,包括路径
					out.write("表单中上传文件路径:"+fileName);out.write("<br>"); 
	        		if(fileName!=null){
	        			
		            	 File fullFile = new File(item.getName());
		            	 String saveFullFileName = fullFile.getName();
		            	 int i = saveFullFileName.lastIndexOf(".");
		            	 System.out.println(i); //不等于-1
		            	 if(i!=-1){
		            		 String postfix = saveFullFileName.substring(i);
			            	 System.out.println("文件后缀:"+postfix); 
			            	 File savedFile = new File(uploadPath,"GREE"+fullFile.getName()); //在这里修改文件名
			            	 item.write(savedFile);
		            	 }else{
		            		 System.out.println("不能识别的文件");
		            	 }
		            	 
		            	 
		         	}
	        		
	        	}
	         	
	        }
	        out.print("upload succeed");
      
      }catch(Exception e){
         e.printStackTrace();
         out.print(e.getMessage());
      }

  }else{
      out.println("the enctype must be multipart/form-data");
  }

%>

<html>
<head>
<title>File upload</title>
</head>
<body>
</body>
</html>

3、下载功能

@ResponseBody
	@RequestMapping(value="/download.xhtml")
	public void download(Model model,
			HttpSession session,
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="filename", required=true) String filename){
		
		//String filename = "1392876828781.msg";   
        // String online = request.getParameter("online");  
        
        FileInputStream in = null;   
        ServletOutputStream out = null;   
        try {  
        	
            response.setContentType("application/x-msdownload");   
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);   
          
            in = new FileInputStream("D:\\sjgg\\"+filename);   
            out = response.getOutputStream();  
            out.flush();  
            int aRead = 0;  
            while ((aRead = in.read()) != -1 & in != null) {  
                out.write(aRead);  
             }  
            out.flush();  
              
        } catch (Throwable e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                in.close();  
                out.close();  
            } catch (Throwable e) {  
                e.printStackTrace();  
            }  
        }  
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值