现在的位置: 首页 > 综合 > 正文

Servlet实现上传

2018年07月13日 ⁄ 综合 ⁄ 共 2353字 ⁄ 字号 评论关闭

秒速赛车公式 www.l19l7.cn

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import java.io.File;


public class Test extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPostAndGet(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPostAndGet(request, response);
	}
	
	public void doPostAndGet(HttpServletRequest request, HttpServletResponse response)
	{
		boolean isMultipart= ServletFileUpload.isMultipartContent(request);
        if(isMultipart){
            //构造一个文件上传处理对象
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            
            Iterator    items;
            try{
                //解析表单中提交的所有文件内容
                items=upload.parseRequest(request).iterator();
                while(items.hasNext()){
                    FileItem item = (FileItem) items.next();
                    if(!item.isFormField()){
                        //取出上传文件的文件名称
                        String name = item.getName();
                        //取得上传文件以后的存储路径
                        String fileName=name.substring(name.lastIndexOf('\\')+1,name.length());
                        //上传文件以后的存储路径
                        String path= request.getRealPath("file")+ File.pathSeparator + fileName;
                        
                        //上传文件
                        File uploadFile = new File(path);
                        item.write(uploadFile);
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
	}
}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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>
    This is my JSP page. <br>
    <form action="Test" method="post" enctype ="multipart/form-data">
    	<input type="file" name="UpLoadFile">
    	<input type="submit">
    </form>
  </body>
</html>

抱歉!评论已关闭.

  • 听,盲童唱出心底的阳光 2018-12-17
  • 老婆告老公索债780万 原是二人自导自演 2018-12-17
  • 井冈山交警开展重点车辆严重交通违法行为有奖举报工作 2018-12-17
  • 停车收费新政首日举报量攀升 2018-12-17
  • 这是世界杯开赛当晚的广西 2018-12-16
  • 【理上网来·喜迎十九大】建设世界一流军队的科学指南 2018-12-16
  • 第六届北京农业嘉年华--北京频道--人民网 2018-12-16
  • 【专题】节能降耗 保卫蓝天——浙江省暨杭州市2018年节能宣传周 2018-12-15
  • 【奋斗在新时代】劳道“歹猫”增色互联网“表情” 2018-12-15
  • 驾车撞倒城管队员反复碾压 义乌暴力摊贩被刑拘 2018-12-15
  • 《中国汽车报》2018“西部温暖计划”公益试驾活动即将启程 2018-12-14
  • 奇瑞新能源瑞虎3xe上市 售价8.98万 2018-12-14
  • 几家性价比超高的烤肉店 赶紧去试试 2018-12-14
  • 和“看着就想笑”说说你的“8421” 2018-12-13
  • 中共十八大以来藏语新词术语发布 2018-12-13
  • 863| 127| 478| 274| 815| 202| 44| 619| 278| 17|