ajax上传文件,spring mvc获取文件并处理,通过页面按钮发送url,由后台控制文件下载

前端页面代码:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>加密与解密</title>
		<link rel="stylesheet" href="css/bootstrap.min.css" />
		<link rel="stylesheet" href="css/encrypt.css" />
		<script type="text/javascript" src="js/encrypt.js" ></script>
		<script type="text/javascript" src="js/jquery.min.js" ></script>
	</head>

	<body>
	    <form class="inpform" id="uploadForm" enctype="multipart/form-data">
		<div class="col-lg-6">
			<div class="input-group input-center">
				<input id="t1" type="text" class="form-control" placeholder="请选择需要加密的文件">
				<span class="input-group-btn">
					<input type="file" id="file" name="mpfile" οnchange="getFilePath()" style="filter:alpha(opacity=0);opacity:0;width: 0;height: 0;"/>   
        			<button id="bj1" class="btn btn-default" type="button" οnclick="selectFile();">请选择</button>
      			</span>
			</div>
			<div class="input-group input-center">
				<input id="t2" name="key" type="password" class="form-control" placeholder="请选输入密钥(或随机生成)">
				<span class="input-group-btn">
        			<button id="bj2" class="btn btn-default" type="button" οnclick="KeyRandom()">随机生成</button>
      			</span>
			</div>
			<div class="radio"> 
                <label class="radio-inline">
                    <input type="radio" name="optionsRadiosinline" id="optionsRadios1" value="1" checked> 加密
                </label>
                <label class="radio-inline">
                        <input type="radio" name="optionsRadiosinline" id="optionsRadios2"  value="2"> 解密
                </label>
            </div>
            <div class="radio"> 
                <label class="radio-inline">
                    <input type="radio" name="optionsRadiosinline2" id="optionsRadios3" value="3" checked> ECB模式
                </label>
                <label class="radio-inline">
                        <input type="radio" name="optionsRadiosinline2" id="optionsRadios4"  value="4"> CBC模式
                </label>
            </div>
            <div class="radio"> 
                <label class="radio-inline">
                    <input type="radio" name="optionsRadiosinline3" id="optionsRadios5" value="5" checked> 操作后上传
                </label>
                <label class="radio-inline">
                    <input type="radio" name="optionsRadiosinline3" id="optionsRadios6"  value="6"> 操作后下载
                </label>
            </div>
			<div class="button">
                <button id="btn1" type="button" class="btn btn-primary" οnclick="Save()">上传</button>
            </div>
            <div class="button">
                <span>请先上传文件再下载!</span>
            </div>
            <div class="button">
                <button id="btn2" type="button" class="btn btn-primary" οnclick="DownLoad()">下载</button>
            </div>
		</div>
		  </form>
	</body>

</html>

spring mvc 后台代码:

package com.cczu.sm.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cczu.sm.entity.SM3Digest;
import com.cczu.sm.entity.SM4;
import com.cczu.sm.entity.SM4Utils;
import com.cczu.sm.entity.SM4_Context;
import com.cczu.sm.entity.Util;

@Controller
public class SmController {
    @RequestMapping("/sm")
    @ResponseBody
    public void getFile(String optionsRadiosinline3,
            String optionsRadiosinline2, String optionsRadiosinline,
            String key, MultipartFile mpfile) throws Exception {
        String filename = mpfile.getOriginalFilename();
        String mykey = key;
        String myoption = optionsRadiosinline;
        System.out.println("密钥:" + mykey + "\n" + "文件名:" + filename + "\n"
                + "功能选择:" + myoption);

        byte[] input = mpfile.getBytes();

        //SM3杂凑密钥
        byte[] md = new byte[32];
        byte[] msg1 = mykey.getBytes();
        SM3Digest sm3 = new SM3Digest();
        sm3.update(msg1, 0, msg1.length);
        sm3.doFinal(md, 0);
        String s = new String(Hex.encode(md)).substring(0, 16).toUpperCase();
        //        System.out.println("杂凑密钥:" + s);

        //SM4加密
        //optionsRadiosinline等于1加密,等于2解密
        //optionsRadiosinline2等于3ECB模式,等于4CBC模式
        if (optionsRadiosinline.equals("1")) {
            //SM4设置加密密钥
            SM4 s4 = new SM4();
            SM4_Context ctx = new SM4_Context();
            ctx.isPadding = true;
            ctx.mode = SM4.SM4_ENCRYPT;
            byte[] keyBytes = s.getBytes();
            s4.sm4_setkey_enc(ctx, keyBytes);
            if (optionsRadiosinline2.equals("3")) {
                //ECB模式
                String path = "";
                //等于5上传保存服务器,等于6上传操作后下载
                if (optionsRadiosinline3.equals("5")) {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfile\\";
                } else {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfiledown\\";
                }
                File file = new File(path + filename + ".encecb");
                if (file.exists()) {
                    file.delete();
                    file.createNewFile();
                }
                FileOutputStream out = new FileOutputStream(file);
                out.write(s4.sm4_crypt_ecb(ctx, input), 0,
                    s4.sm4_crypt_ecb(ctx, input).length);
                out.close();
            } else {
                //CBC模式
                String path = "";
                if (optionsRadiosinline3.equals("5")) {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfile\\";
                } else {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfiledown\\";
                }
                File file = new File(path + filename + ".enccbc");
                FileOutputStream out = new FileOutputStream(file);
                out.write(s4.sm4_crypt_cbc(ctx, keyBytes, input), 0,
                    s4.sm4_crypt_cbc(ctx, keyBytes, input).length);
                out.close();
            }
        } else {
            //解密操作
            //SM4设置加密密钥
            SM4 s4 = new SM4();
            SM4_Context ctx = new SM4_Context();
            ctx.isPadding = true;
            ctx.mode = SM4.SM4_DECRYPT;
            byte[] keyBytes = s.getBytes();
            s4.sm4_setkey_dec(ctx, keyBytes);
            if (optionsRadiosinline2.equals("3")) {
                //ECB模式
                String path = "";
                //等于5上传保存服务器,等于6上传操作后下载
                if (optionsRadiosinline3.equals("5")) {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfile\\";
                } else {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfiledown\\";
                }
                File file = new File(path
                        + filename.substring(0, filename.lastIndexOf(".")));
                if (file.exists()) {
                    file.delete();
                    file.createNewFile();
                }
                FileOutputStream out = new FileOutputStream(file);
                out.write(s4.sm4_crypt_ecb(ctx, input), 0,
                    s4.sm4_crypt_ecb(ctx, input).length);
                out.close();
            } else {
                //CBC模式
                String path = "";
                if (optionsRadiosinline3.equals("5")) {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfile\\";
                } else {
                    path = "F:\\MyEclipse 9Workspaces\\sm\\smfiledown\\";
                }
                File file = new File(path
                        + filename.substring(0, filename.lastIndexOf(".")));
                FileOutputStream out = new FileOutputStream(file);
                out.write(s4.sm4_crypt_cbc(ctx, keyBytes, input), 0,
                    s4.sm4_crypt_cbc(ctx, keyBytes, input).length);
                out.close();
            }
        }

    }

   
    @RequestMapping("/sm/download")
    public void downLoad(String file, HttpServletRequest request,
            HttpServletResponse response) {
        String path = "F:\\MyEclipse 9Workspaces\\sm\\smfiledown\\";
        File ofile = new File(path);
        for (File p : ofile.listFiles()) {
            if (!p.isDirectory()) {
                String fname = p.getName();
                FileInputStream fs = null;
                if (p.exists()) {
                    // response.setContentType("application/force-download");
                    try {
                        response.setHeader(
                            "Content-Disposition",
                            "attachment;fileName="
                                    + new String(fname.getBytes("utf-8"),
                                            "iso_8859_1"));
                        fs = new FileInputStream(p);
                        byte[] buf = new byte[1024];
                        int len = 0;
                        ServletOutputStream o = response.getOutputStream();
                        while ((len = fs.read(buf)) != -1) {
                            o.write(buf, 0, len);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        request.setAttribute("error", " 쳣  ");
                    } finally {
                        try {
                            if (fs != null)
                                fs.close();
                        } catch (IOException e) {
                            request.setAttribute("error", " 쳣  ");
                        }
                    }
                   p.delete();
                    break;
                }
            }
        }
    }

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Walter Sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值