学习总结7/29

文件上传

前端代码
servlet:
package com.base.only.day728;

import java.io.File;
import java.io.IOException;
import java.util.List;

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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class UplodSerxlet
 */
public class UplodSerxlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UplodSerxlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DiskFileItemFactory factory = new DiskFileItemFactory(); 
	    //当内存中文件数据达到THRESHOLD_SIZE后,就写入临时文件中,避免上传大文件时,消化太多内存 
	      
	    ServletFileUpload upload = new ServletFileUpload(factory); 
	    upload.setHeaderEncoding("UTF-8"); 
	    upload.setSizeMax(2*1024*1024); 
	    String savePath="C:/tmp/";
	    try {
	    	//获取from表单里面,所有的元素
			List items = upload.parseRequest(request);
			//from 循环遍历表单里面所有元素
			for(int i=0;i<items.size();i++){
				//每个表单元素都分装到一个FileItem中
				FileItem fitem=(FileItem) items.get(i);
				System.out.println("元素name属性的值="+fitem.getFieldName());
				//fitem.isFormField()表示如果当前这个元素类型TYPE不等于file,返回TRUE
			if(	fitem.isFormField()){
				System.out.println("这个一个普通的非文件字段");
				System.out.println("输入框前段输入的值是==="+fitem.getString());
				
			}else{
				System.out.println("这个一个文件字段");
				System.out.println("原始的文件名字"+fitem.getName());
				//File.separator是一个静态变量,在windows的操作系统返回是\\linux是/
				//存储上传的文件
				String filename=System.currentTimeMillis()+"-"+fitem.getName();
				File file=new File(savePath+File.separator+filename);
				try {
					fitem.write(file);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//doGet(request, response);
	}

}

Html:

<!DOCTYPE html>
<html>
<head>
  <title>登录</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="row">
<div class="col-sm-5"></div>
<div class="col-sm-4"><h1>登录</h1></div>
<div class="col-sm-3"></div>
</div>

<div class="row">
  <div class="col-sm-3"></div>
 	 <div class="col-sm-6">
		<form action="LoginServlet">
		  <div class="form-group">
		    <label for="email">用户名:</label>
		    <input type="text" class="form-control" id="email" name="uname">
		  </div>
		  <div class="form-group">
		    <label for="pwd">密码:</label>
		    <input type="password" class="form-control" id="pwd" name="password">
		  </div>
		 <button type="submit" class="btn btn-primary">登录</button>
		</form>
	 </div>
	 
	  <div class="col-sm-3"></div>
</div>
</body>
</html>

查询数据库的方法

package com.base.only.day728;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.base.only.day726.JDBCComon;


public class HomeworkSer {
	JDBCComon jdbc=new JDBCComon();
	Connection conn=null;
	public ArrayList<UserHkdesc> getHomeworkdetail(){
		ArrayList<UserHkdesc> ls=new ArrayList<UserHkdesc>();
		try {
			conn=jdbc.getConnection();
			String sql="select u.name,count(homework_id) frequency from user u left join user_homework uh  on (u.id=uh.user_id ) group by u.name order by count(homework_id)desc";
			PreparedStatement ps=conn.prepareStatement(sql);
		
			ResultSet rs=ps.executeQuery();
			int a=0;
			while(rs.next()){
				UserHkdesc hk=new UserHkdesc();												
				hk.setName(rs.getString("name"));
				hk.setFrequency(rs.getInt("frequency"));				
				a=a+1;
				hk.setTop(a);
				ls.add(hk);
			}
			 for(int i=0;i<ls.size();i++){
				 System.out.println(	ls.get(i).getTop()+	 ls.get(i).getName()+ ls.get(i).getFrequency());
			 }
			rs.close();
			ps.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ls;
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值