文件上传

2021-06-27 03:06

阅读:450

YPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

标签:str   control   html 4.01   rect   文件类型   source   txt   content   todo   

 

1.上传的步骤:

  a.导入SmartUpload.jar

  b.创建一个上传的类的对象

  c.初始化

  d.上传至服务器

  e.保存

表单提交时需要指定enctype="multipart/form-data"(多数据类型提交)

 http://www.atguigu.com/opensource.shtml#3(包下载地址)

package com.zuxia.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**

*文件上传

*/

public class UploadServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      //创建一个上传/下载的类的对象
      SmartUpload su = new SmartUpload();
      //初始化对象
      su.initialize(getServletConfig(), request, response);
      try {
        //设置上传文件的信息(文件大小、文件类型)
        //设置允许上传的文件的类型
        su.setAllowedFilesList("txt,jpg,png,gif,mp3");
        //设置不允许上传的文件的类型
        su.setDeniedFilesList("exe");
        //设置文件上传的大小限制
        su.setMaxFileSize(1024*1024*2);//2M字节数
        //上传文件至服务器
        su.upload();
        //指定文件保存路径
        su.save("/files");
        System.out.println("上传成功!");
        response.sendRedirect("../show.jsp");
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
   }

}

 

 

/*index.jsp*/

 


String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




My JSP ‘index.jsp‘ starting page




















文件上传

标签:str   control   html 4.01   rect   文件类型   source   txt   content   todo   

原文地址:http://www.cnblogs.com/shishaohui/p/7148018.html


评论


亲,登录后才可以留言!