码迷,mamicode.com
首页 > Web开发 > 详细

servlet上传与下载

时间:2017-10-19 21:16:39      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:read   -name   max   ted   bsp   hold   tput   upload   extends   

上传页面

上传学生信息

学号 
姓名 
密码 
性别 
年龄 
身高 
学院 
照片 
简历 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传学生信息</title>
</head>
<body>
    <form action="/lib/upload" method="post" enctype="multipart/form-data">
        学号&nbsp;<input type="text" name="xuehao" id="xuehao"/>
        <br/>
        姓名&nbsp;<input type="text" name="xingming" id="xingming">
        <br/>
        密码&nbsp;<input type="password" name="mima" id="mima">
        <br/>
        性别&nbsp;<input type="radio" name="xingbie" value="nan" checked><input type="radio" name="xingbie" value="nv" ><br/>
        年龄&nbsp;<input type="text" name="nianling" id="nianling">
        <br/>
        身高&nbsp;<input type="text" name="shengao" id="shengao">
        <br/>
        学院&nbsp;<select name="xueyuan">
        <option value="computer">计算机学院</option>
        <option value="software">软件学院</option>
    </select>
        <br/>
        照片&nbsp;<input type="file" name="zhaopian" id="zhaopian"/>
        <br/>
        简历&nbsp;<input type="file" name="jianli" id="jianli">
        <br/>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

 

上传需要的配置信息web.xml

<servlet>
    <description></description>
    <display-name>UploadServlet</display-name>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>com.sangewuxie.servlet.UploadServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
    <multipart-config>
      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>
      <file-size-threshold>0</file-size-threshold>
    </multipart-config>
  </servlet>
  <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/upload</url-pattern>
  </servlet-mapping>
  <servlet>

上传的servlet

package com.sangewuxie.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.sangewuxie.dao.StudentDao;
import com.sangewuxie.domain.Student;

public class UploadServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        String xuehao = request.getParameter("xuehao");
        String xingming = request.getParameter("xingming");
        String mima = request.getParameter("mima");
        String xingbie = request.getParameter("xingbie");
        int nianling = Integer.parseInt(request.getParameter("nianling"));
        int shengao = Integer.parseInt(request.getParameter("shengao"));
        String xueyuan = request.getParameter("xueyuan");
        String dir = request.getServletContext().getRealPath("");
        
        Part zhaopianPart = request.getPart("zhaopian");
        String zhaopian = upload(request, zhaopianPart, dir+"\\upload\\zhaopian");
        Part jianliPart =request.getPart("jianli"); 
        String jianli = upload(request, jianliPart, dir+"\\upload\\jianli");
        Student student = new Student(xuehao, xingming, mima, xingbie, nianling, shengao, xueyuan, zhaopian, jianli);
        StudentDao.InsertStudentInfo(student);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("上传成功");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
    public String upload(HttpServletRequest request,Part part,String dir) throws IOException {
        String cd = part.getHeader("content-disposition");
        /**
         * tomcat7没有getSubmittedFileName()方法
      * String filename = part.getSubmittedFileName()
*/ String filename = cd.substring(cd.lastIndexOf("=")+2, cd.length()-1); String houzhui = filename.substring(filename.lastIndexOf(‘.‘)); String destfilename = System.currentTimeMillis()+""; destfilename = destfilename + houzhui; part.write(dir+"\\"+destfilename); // InputStream ist = part.getInputStream(); // FileOutputStream fos = new FileOutputStream(dir+"\\"+destfilename); // byte[] bytes = new byte[1024]; // int size = -1; // while((size = ist.read(bytes))>0){ // fos.write(bytes, 0, size); // } // ist.close(); // fos.close(); return destfilename; } }

 

下载  流程

package com.sangewuxie.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;


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

import com.sangewuxie.dao.StudentDao;

public class DownloadServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String xuehao = request.getParameter("xuehao");
        String jianli = StudentDao.getName(xuehao,"jianli");
//        System.out.println(jianli);
        
        if(jianli==null) {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("未找到该生或该生不存在");
        }
        
        else {
            String contentType = getServletContext().getMimeType(jianli);
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "attachment;filename="+jianli);
            String dir = request.getServletContext().getRealPath("")+"\\upload\\jianli\\";
            String filename = dir+jianli;
            InputStream in = new FileInputStream(filename);
            OutputStream out = response.getOutputStream();  
            int len;  
            while((len=in.read())!= -1)  
            {  
                out.write(len);  
            }  
              
            in.close();  
            out.close();  
        }
    }

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

}

 

保存到csv

package com.sangewuxie.servlet;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;

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

import com.sangewuxie.dao.StudentDao;
import com.sangewuxie.domain.Student;


public class SavecsvServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int minAge = Integer.parseInt(request.getParameter("min"));
        int maxAge = Integer.parseInt(request.getParameter("max"));
        ArrayList<Student> students= StudentDao.getInfo(minAge,maxAge);
        
        response.setContentType("text/html;charset=utf-8");
        if(students.isEmpty()) {
            response.getWriter().write("未找到该数据或者数据不存在");
        }
        
        else {
            
            String filename = "从"+minAge+"到"+maxAge+"岁的信息.csv";
            filename = URLEncoder.encode(filename,"UTF-8");
            String contentType =getServletContext().getMimeType(filename);
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "attachment;filename="+filename);
            
            OutputStream out = response.getOutputStream(); 
            PrintStream ps = new PrintStream(out);
            ps.println("学号,姓名,性别,年龄,身高,学院");
            Iterator it = students.iterator();  
            while(it.hasNext()) {
                Student  student= (Student) it.next();
                ps.println(student.getXuehao()+","+student.getXingming()+","+student.getXingbie()+","+student.getNianling()+","+student.getShengao()+","+student.getXueyuan());
            }
            ps.flush();
            ps.close();
            out.close();
        }            
    }

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

}

 

 

 

 

个人笔记。无需细看

servlet上传与下载

标签:read   -name   max   ted   bsp   hold   tput   upload   extends   

原文地址:http://www.cnblogs.com/sangewuxie/p/7694655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!