MyEclipse------如何添加jspsmartupload.jar+文件上传到服务器

下载地址:http://download.csdn.net/detail/heidan2006/182263

如何添加jspsmartupload.jar:
右键“Web”工程-》properties-》Libraries-》Add External JARs...-》找到“jspsmartupload.jar”,添加进去
-》点击“OK”-》找到“E:MyEclipse2014projectsWebWebRootWEB-INFlib”路径,把“jspsmartupload.jar”添加进去
-》重启tomcat,不然出现invalid

uploadForm.html

<!DOCTYPE html>
<html>
  <head>
    <title>uploadForm.html</title>
    
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=gbk">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    <form action="usingsmartupload.jsp" method="post" enctype="multipart/form-data" name="theFile">
        <input type="file" name="file" size="30"/><br>
        <input type="submit" value="上传" name="btn">
    </form>
  </body>
</html>

usingsmartupload.jsp

<%@page import="java.io.File"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!-- java.sql.*包可以不导 -->
<%@page import="java.sql.*" %>
<%@page import="com.jspsmart.upload.*" %>
<%@page import="java.io.*" %>
<jsp:useBean id="the" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingsmartupload.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">
    
    <!-- 这段代码可以不加 -->
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
    
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%
        try{
            the.initialize(pageContext);
            the.setTotalMaxFileSize(5*1024*1024);
            the.upload();
            String fn=the.getFiles().getFile(0).getFieldName();
            
            path=request.getRealPath("")+"\myfile";
            //java.io.File d=new java.io.File(path);
            File d=new File(path);
            if(!d.exists()){
                d.mkdirs();
            }
            
            the.save(path);
            //the.save("E:\FFOutput\");
            out.print(fn);
            out.print("文件已上传成功!!!");
        }
        catch(Exception e){
            e.printStackTrace();
        }
     %>
  </body>
</html>

方法二:通过跳转Servlet上传

public class upfile extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public upfile() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

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

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        request.setCharacterEncoding("gb2312");
        response.setContentType("text/html;charset=gb2312");
        
        SmartUpload up=new SmartUpload();
        
        try{
            /*
             第一个参数是传递一个Servlet,在servlet中传递this就可以了;
            第二个和第三个参数是request与response不多说明了;
            第四个参数是发生错误后的url路径地址,如果没有可以键入null;
            第五个参数是是否需要session,这里可以写入true;
            第六个参数是缓存大小,我们用了8*1024;
            第七个蚕食是是否需要刷新,键入ture;*/
            PageContext context = JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8*1024, true);
            up.initialize(context);
            up.setTotalMaxFileSize(5*1024*1024); //上传的总文件大小不能超过这个
            up.upload();
            
            //获取文件名
            //String fn=up.getFiles().getFile(0).getFieldName();
            
            String path="E:\Tomcat\webapps\MyWeb\myfile";
            //java.io.File d=new java.io.File(path);
            File d=new File(path);
            if(!d.exists()){
                d.mkdirs();
            }
            up.save(path);
            //System.out.print(fn);
            //System.out.print("文件上传成功啦!!!");
            RequestDispatcher dispatcher=request.getRequestDispatcher("/splitpage2/downfile.jsp");
            dispatcher.forward(request, response);
            
            //无法传参
            //response.sendRedirect("http://localhost:8080/MyWeb/splitpage2/list.jsp");
            }
            catch(Exception e){
                e.printStackTrace();
            }
        
    }

    public void init() throws ServletException {
        // Put your code here
    }

}
原文地址:https://www.cnblogs.com/tianhengblogs/p/5316413.html