• java框架--springmvc --ajax-json-upload/download+maven+ DES/MD5 请求加密


     注:eclipse 开发工具  <context:component-scan base-package="com.zhouwuji.controller"></context:component-scan> 加入后 controller包的控制类会有S符好

           commons io 和commons upload 开发

            FTP服务器创建和上下传文件(3)

           httpclient调用代码

    package com.zhouwuji.controller;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.URLEncoder;
    import java.util.Iterator;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.io.FileUtils;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    import org.springframework.web.multipart.commons.CommonsMultipartFile;
    import org.springframework.web.multipart.commons.CommonsMultipartResolver;
    
    @Controller
    public class FileController {
        @RequestMapping("/download.do")
        public void doMain(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            //得到要下载的文件名
             String fileName =request.getParameter("filename"); 
             System.out.println("fileName:"+fileName.trim());
             //23239283-92489-阿凡达.avi
             // fileName="zhangruan.txt";
             // fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
            //上传的文件都是保存在/WEB-INF/upload目录下的子目录当中
            String fileSaveRootPath=request.getServletContext().getRealPath("/WEB-INF/upload");
            //得到要下载的文件
            File file = new File(fileSaveRootPath + "\" + fileName);
            //如果文件不存在
            if(!file.exists()){
                request.setAttribute("message", "您要下载的资源已被删除!!");
                request.getRequestDispatcher("/fail.jsp").forward(request, response);
                return;
            }
            //处理文件名
            String realname = fileName.substring(fileName.indexOf(".")+1);
            //设置响应头,控制浏览器下载该文件
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
            //读取要下载的文件,保存到文件输入流
              
            FileInputStream in = new FileInputStream(file);
            //创建输出流
            OutputStream out = response.getOutputStream();
            //创建缓冲区
            byte buffer[] = new byte[1024];
            int len = 0;
            //循环将输入流中的内容读取到缓冲区当中
            while((len=in.read(buffer))>0){
                //输出缓冲区的内容到浏览器,实现文件下载
                out.write(buffer, 0, len);
            }
            System.out.println("下载成功");
            //关闭文件输入流
            in.close();
            //关闭输出流
            out.close();
        }
        
        
        
        @RequestMapping("/upload.do")   //@RequestParam("uploadfile")
        @ResponseBody
        public String queryFileData(@RequestParam("uploadfile")CommonsMultipartFile file,HttpServletRequest request){
            System.out.println("+++++++++++++++++"+request.getParameter("username"));
            /*MultipartFile是对当前上传的文件的封装,
             * 当同时上传多个文件时,可以给定多个MultipartFile参数(数组)
             */
            if(file!=null){
                System.out.println("文件对象接到了!");
                //获取文件名
                String filename=file.getOriginalFilename();
                //获取上传路径
                String path=request.getSession().getServletContext().getRealPath("/WEB-INF/upload/"+filename.trim());
                System.out.println("path:"+path);
                
                //创建文件流并指定写入路径
                File destFile=new File(path);
                
                    //springmvc的方式
                    //该方法自动操作,不需要额外的去关闭IO流
                    //复制临时文件到指定文件夹下
                    try {
                        FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
                        System.out.println("上传成功");
                        return "/success.jsp";
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("上传失败");
                        return "/error.jsp";
                    }
            }else{
                System.out.println("文件没有对象接到了!");
                return "/error.jsp";
                
            }
        }
     }
    /*文件对象接到了!
    filename:中软笔记.txt
    path:D:/apache-tomcat-7.0.82-windows-x64/apache-tomcat-7.0.82/webapps/project-ajaxjson/upload/中软笔记.txt
    上传成功*/
    FileController
    package com.zhouwuji.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    
    
    
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.zhouwuji.util.EncryptUtil;
    
    @Controller
    public class LoginController {
          
        @RequestMapping("/json.do")
        @ResponseBody
        public  List<User> login(HttpServletRequest request) throws Exception{
            String  username=request.getParameter("username");
            String  password=request.getParameter("password");
            // key 为解密密钥
            String   key=request.getParameter("key");
            if (username !=null && password !=null ) {
                System.out.println("username:"+EncryptUtil.decrypt(username, key));
                System.out.println("password:"+EncryptUtil.decrypt(password, key));
                
            }
            if (key==null) {
                key="LmMGStGtOpF4xNyvYt54EQ==";
            }
            List<User> users=new ArrayList<User>();
            users.add(new User(1, EncryptUtil.encrypt("张三",key), "男"));
            users.add(new User(2, EncryptUtil.encrypt("李四",key), "男"));
            users.add(new User(3, EncryptUtil.encrypt("西施",key), "女"));
            return users;
        }
    }
    LoginController 
    package com.zhouwuji.controller;
    
    public class User {
        private int id;
        private String name;
        private String sex;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public User(int id, String name, String sex) {
            super();
            this.id = id;
            this.name = name;
            this.sex = sex;
        }
        public User() {
            super();
        }
        
    }
    User
    package com.zhouwuji.util;
    
    import java.io.IOException;  
    import java.io.UnsupportedEncodingException;  
    import java.security.MessageDigest;  
      
    
    import javax.crypto.Cipher;  
    import javax.crypto.spec.IvParameterSpec;  
    import javax.crypto.spec.SecretKeySpec;  
      
    
    import sun.misc.BASE64Decoder;  
    import sun.misc.BASE64Encoder;  
      
    /** 
     * 功能描述  DES+MD5 加密工具类
     * 加密常用类 
     */  
    public class EncryptUtil {  
        // 密钥是16位长度的byte[]进行Base64转换后得到的字符串  
       // public static String key = "LmMGStGtOpF4xNyvYt54EQ==";  
       
        /** 
         * <li> 
         * 方法名称:encrypt</li> <li> 
         * 加密方法 
         * @param xmlStr 
         *            需要加密的消息字符串 
         * @return 加密后的字符串 
         */  
        public static String encrypt(String xmlStr,String key) {  
            byte[] encrypt = null;  
        
            try {  
                // 取需要加密内容的utf-8编码。  
                encrypt = xmlStr.getBytes("utf-8");  
            } catch (UnsupportedEncodingException e) {  
                e.printStackTrace();  
            }  
            // 取MD5Hash码,并组合加密数组  
            byte[] md5Hasn = null;  
            try {  
                md5Hasn = EncryptUtil.MD5Hash(encrypt, 0, encrypt.length);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            // 组合消息体  
            byte[] totalByte = EncryptUtil.addMD5(md5Hasn, encrypt);  
      
            // 取密钥和偏转向量  
            byte[] key1 = new byte[8];  
            byte[] iv = new byte[8];  
            getKeyIV(key, key1, iv);  
            SecretKeySpec deskey = new SecretKeySpec(key1, "DES");  
            IvParameterSpec ivParam = new IvParameterSpec(iv);  
      
            // 使用DES算法使用加密消息体  
            byte[] temp = null;  
            try {  
                temp = EncryptUtil.DES_CBC_Encrypt(totalByte, deskey, ivParam);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
            // 使用Base64加密后返回  
            return new BASE64Encoder().encode(temp);  
        }  
      
        /** 
         * <li> 
         * 方法名称:encrypt</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * 解密方法 
         * </pre> 
         *  
         * </li> 
         *  
         * @param xmlStr 
         *            需要解密的消息字符串 
         * @return 解密后的字符串 
         * @throws Exception 
         */  
        public static String decrypt(String xmlStr,String key) throws Exception {  
            // base64解码  
            BASE64Decoder decoder = new BASE64Decoder();  
            byte[] encBuf = null;  
            try {  
                encBuf = decoder.decodeBuffer(xmlStr);  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
      
            // 取密钥和偏转向量  
            byte[] key1 = new byte[8];  
            byte[] iv = new byte[8];  
            getKeyIV(key, key1, iv);  
      
            SecretKeySpec deskey = new SecretKeySpec(key1, "DES");  
            IvParameterSpec ivParam = new IvParameterSpec(iv);  
      
            // 使用DES算法解密  
            byte[] temp = null;  
            try {  
                temp = EncryptUtil.DES_CBC_Decrypt(encBuf, deskey, ivParam);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
            // 进行解密后的md5Hash校验  
            byte[] md5Hash = null;  
            try {  
                md5Hash = EncryptUtil.MD5Hash(temp, 16, temp.length - 16);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
            // 进行解密校检  
            for (int i = 0; i < md5Hash.length; i++) {  
                if (md5Hash[i] != temp[i]) {  
                    // System.out.println(md5Hash[i] + "MD5校验错误。" + temp[i]);  
                    throw new Exception("MD5校验错误。");  
                }  
            }  
      
            // 返回解密后的数组,其中前16位MD5Hash码要除去。  
            return new String(temp, 16, temp.length - 16, "utf-8");  
        }  
      
        /** 
         * <li> 
         * 方法名称:TripleDES_CBC_Encrypt</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * 经过封装的三重DES/CBC加密算法,如果包含中文,请注意编码。 
         * </pre> 
         *  
         * </li> 
         *  
         * @param sourceBuf 
         *            需要加密内容的字节数组。 
         * @param deskey 
         *            KEY 由24位字节数组通过SecretKeySpec类转换而成。 
         * @param ivParam 
         *            IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。 
         * @return 加密后的字节数组 
         * @throws Exception 
         */  
        public static byte[] TripleDES_CBC_Encrypt(byte[] sourceBuf,  
                SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
            byte[] cipherByte;  
            // 使用DES对称加密算法的CBC模式加密  
            Cipher encrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");  
      
            encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam);  
      
            cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
            // 返回加密后的字节数组  
            return cipherByte;  
        }  
      
        /** 
         * <li> 
         * 方法名称:TripleDES_CBC_Decrypt</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * 经过封装的三重DES / CBC解密算法 
         * </pre> 
         *  
         * </li> 
         *  
         * @param sourceBuf 
         *            需要解密内容的字节数组 
         * @param deskey 
         *            KEY 由24位字节数组通过SecretKeySpec类转换而成。 
         * @param ivParam 
         *            IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。 
         * @return 解密后的字节数组 
         * @throws Exception 
         */  
        public static byte[] TripleDES_CBC_Decrypt(byte[] sourceBuf,  
                SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
      
            byte[] cipherByte;  
            // 获得Cipher实例,使用CBC模式。  
            Cipher decrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");  
            // 初始化加密实例,定义为解密功能,并传入密钥,偏转向量  
            decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam);  
      
            cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
            // 返回解密后的字节数组  
            return cipherByte;  
        }  
      
        /** 
         * <li> 
         * 方法名称:DES_CBC_Encrypt</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * 经过封装的DES/CBC加密算法,如果包含中文,请注意编码。 
         * </pre> 
         *  
         * </li> 
         *  
         * @param sourceBuf 
         *            需要加密内容的字节数组。 
         * @param deskey 
         *            KEY 由8位字节数组通过SecretKeySpec类转换而成。 
         * @param ivParam 
         *            IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。 
         * @return 加密后的字节数组 
         * @throws Exception 
         */  
        public static byte[] DES_CBC_Encrypt(byte[] sourceBuf,  
                SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
            byte[] cipherByte;  
            // 使用DES对称加密算法的CBC模式加密  
            Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");  
      
            encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam);  
      
            cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
            // 返回加密后的字节数组  
            return cipherByte;  
        }  
      
        /** 
         * <li> 
         * 方法名称:DES_CBC_Decrypt</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * 经过封装的DES/CBC解密算法。 
         * </pre> 
         *  
         * </li> 
         *  
         * @param sourceBuf 
         *            需要解密内容的字节数组 
         * @param deskey 
         *            KEY 由8位字节数组通过SecretKeySpec类转换而成。 
         * @param ivParam 
         *            IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。 
         * @return 解密后的字节数组 
         * @throws Exception 
         */  
        public static byte[] DES_CBC_Decrypt(byte[] sourceBuf,  
                SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
      
            byte[] cipherByte;  
            // 获得Cipher实例,使用CBC模式。  
            Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");  
            // 初始化加密实例,定义为解密功能,并传入密钥,偏转向量  
            decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam);  
      
            cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
            // 返回解密后的字节数组  
            return cipherByte;  
        }  
      
        /** 
         * <li> 
         * 方法名称:MD5Hash</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * MD5,进行了简单的封装,以适用于加,解密字符串的校验。 
         * </pre> 
         *  
         * </li> 
         *  
         * @param buf 
         *            需要MD5加密字节数组。 
         * @param offset 
         *            加密数据起始位置。 
         * @param length 
         *            需要加密的数组长度。 
         * @return 
         * @throws Exception 
         */  
        public static byte[] MD5Hash(byte[] buf, int offset, int length)  
                throws Exception {  
            MessageDigest md = MessageDigest.getInstance("MD5");  
            md.update(buf, offset, length);  
            return md.digest();  
        }  
      
        /** 
         * <li> 
         * 方法名称:byte2hex</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * 字节数组转换为二行制表示 
         * </pre> 
         *  
         * </li> 
         *  
         * @param inStr 
         *            需要转换字节数组。 
         * @return 字节数组的二进制表示。 
         */  
        public static String byte2hex(byte[] inStr) {  
            String stmp;  
            StringBuffer out = new StringBuffer(inStr.length * 2);  
      
            for (int n = 0; n < inStr.length; n++) {  
                // 字节做"与"运算,去除高位置字节 11111111  
                stmp = Integer.toHexString(inStr[n] & 0xFF);  
                if (stmp.length() == 1) {  
                    // 如果是0至F的单位字符串,则添加0  
                    out.append("0" + stmp);  
                } else {  
                    out.append(stmp);  
                }  
            }  
            return out.toString();  
        }  
      
        /** 
         * <li> 
         * 方法名称:addMD5</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         * MD校验码 组合方法,前16位放MD5Hash码。 把MD5验证码byte[],加密内容byte[]组合的方法。 
         * </pre> 
         *  
         * </li> 
         *  
         * @param md5Byte 
         *            加密内容的MD5Hash字节数组。 
         * @param bodyByte 
         *            加密内容字节数组 
         * @return 组合后的字节数组,比加密内容长16个字节。 
         */  
        public static byte[] addMD5(byte[] md5Byte, byte[] bodyByte) {  
            int length = bodyByte.length + md5Byte.length;  
            byte[] resutlByte = new byte[length];  
      
            // 前16位放MD5Hash码  
            for (int i = 0; i < length; i++) {  
                if (i < md5Byte.length) {  
                    resutlByte[i] = md5Byte[i];  
                } else {  
                    resutlByte[i] = bodyByte[i - md5Byte.length];  
                }  
            }  
      
            return resutlByte;  
        }  
      
        /** 
         * <li> 
         * 方法名称:getKeyIV</li> <li> 
         * 功能描述: 
         *  
         * <pre> 
         *  
         * </pre> 
         * </li> 
         *  
         * @param encryptKey 
         * @param key 
         * @param iv 
         */  
        public static void getKeyIV(String encryptKey, byte[] key, byte[] iv) {  
            // 密钥Base64解密  
            BASE64Decoder decoder = new BASE64Decoder();  
            byte[] buf = null;  
            try {  
                buf = decoder.decodeBuffer(encryptKey);  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            // 前8位为key  
            int i;  
            for (i = 0; i < key.length; i++) {  
                key[i] = buf[i];  
            }  
            // 后8位为iv向量  
            for (i = 0; i < iv.length; i++) {  
                iv[i] = buf[i + 8];  
            }  
        }  
          
        public static void main(String[] args) throws Exception {  
            System.out.println(encrypt("欧长璐","LmMGStGtOpF4xNyvYt54EQ=="));  
            System.err.println(decrypt("BYyD3pQ9rYonhg9bbbXp3UzZYMGkgmJt8G9NIg1EZ3I=","LmMGStGtOpF4xNyvYt54EQ=="));
        }  
    } 
    EncryptUtil DES、MD5 加密工具类
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@  taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
           值:${ad}  
    </body>
    </html>
    index.jsp
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    
    
        <bean id="stringConverter"
            class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean id="jsonConverter"
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="stringConverter" />
                    <ref bean="jsonConverter" />
                </list>
            </property>
        </bean>
    
    
        <!-- 1.springMVC上传文件时,需要配置CommonsMultipartResolver处理器 -->
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 设置默认的编码格式 -->
            <property name="defaultEncoding" value="UTF-8" />
            <property name="maxInMemorySize" value="10240" /> <!-- 最大内存大小 (10240) -->
            <property name="uploadTempDir" value="/upload/" /> <!-- 上传后的目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
            <!-- 指定所上传文件的总的大小不能超过200kb, 注意maxUploadSize属性点 限制 不是针对单个文件, 而是所有文件的容量总和 -->
            <property name="maxUploadSize" value="-1" /><!-- 最大文件大小,-1为无限止(-1) -->
        </bean>
        <!-- 2.springMVC在超出上传文件限制时,会抛出 org.springframework.web.multipart.MaxUploadSizeExceededException 
            该异常时SpringMVC在检查上传文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
        <bean id="exceptionResolver"
            class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
                    <!-- 在遇到MaxUploadSizeExceededException异常时,自动跳到xxx面 -->
                    <prop
                        key="org.springframework.web.multipart.MaxUploadSizeExceededException">error.jsp</prop>
                </props>
            </property>
        </bean>
    
    
    
        <context:component-scan base-package="com.zhouwuji.controller"></context:component-scan>
    </beans>
    springmvc-servlet.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
       <filter>
          <filter-name>encoding</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
          <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
          </init-param>
        </filter>
        <filter-mapping>
          <filter-name>encoding</filter-name>
          <url-pattern>/*</url-pattern>
        </filter-mapping>
       
       <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
       
       <welcome-file-list>
               <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
    </web-app>
    web.xml
    <%@ 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>
       文件上传异常
      </body>
    </html>
    error.jsp
    <%@ 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>
       文件上传失败
      </body>
    </html>
    fail.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@  taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <center>
            <form action="<%=basePath%>upload.do" method="post"
                enctype="multipart/form-data">
                <input type="hidden" name="holly" value="holly" /> 上传文件:<input
                    type="file" name="uploadfile"> <input type="submit"
                    value="上传">
            </form>
            <button id="btn" type="button">Click Me!</button>
            <table width="80%" align="center">
                <tr>
                    <td>编号</td>
                    <td>姓名</td>
                    <td>性别</td>
                </tr>
                <tbody id="content">
                </tbody>
            </table>
        </center>
    </body>
    <script src="js/jquery-1.8.3.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            $("#btn").click(
                    function() {
                        $.post("json.do", function(data) {
                            var html = "";
                            for (var i = 0; i < data.length; i++) {
                                html += "<tr><td>" + data[i].id + "</td><td>"
                                        + data[i].name + "</td><td>" + data[i].sex
                                        + "</td></tr>";
                            }
                            ;
                            $("#content").html(html);
                        });
                    });
        });
    </script>
    </html>
    index.jsp
    <%@ 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>
       文件上传成功
      </body>
    </html>
    success.jsp
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.zhouwuji.wuji</groupId>
        <artifactId>project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
            <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.6.RELEASE</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>4.1.6.RELEASE</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/jstl/jstl -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.5.4</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.5.4</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.5.4</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.2.2</version>
            </dependency>
    
        </dependencies>
    
    
    </project>
    pom.xml
  • 相关阅读:
    [BZOJ 1095] [ZJOI 2007]Hide 捉迷藏
    [BZOJ 2759] 一个动态树好题
    BZOJ 3122 SDOI2013 随机数生成器
    [NOIP集训]10月18日
    [NOIP集训]10月17日
    [NOIP集训]10月16日
    [NOI题库]1.3编程基础之算术表达式与顺序执行 题解(一)
    [NOI题库]1.2编程基础之变量定义、赋值及转换 题解
    [NOI题库]1.1编程基础之输入输出 题解
    [作业]排序算法练习(二)
  • 原文地址:https://www.cnblogs.com/ou-pc/p/8241771.html
Copyright © 2020-2023  润新知