冲刺1

1.实现了对文件的拖拽上传

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <title>上传文件</title>
    <link rel="stylesheet" href="css/layui.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<style>
    body{ text-align:center}
    div{
        border : 1px solid black;
        margin : 10px 20px 30px 40px;
    }
    .divcss5{margin:100px auto;opacity:0.5;color:#333333;background-color:whitesmoke;width:700px;min-height:500px;height:auto; overflow:hidden;border:3px dashed silver;}
    .center-in-center{
        margin-top: 20%;
        margin-left: 50%;
        -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
        -o-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
    }
    /*样式2*/
    .file {
        position: relative;
        display: inline-block;
        background: #D0EEFF;
        border: 1px solid #99D3F5;
        border-radius: 4px;
        padding: 4px 12px;
        overflow: hidden;
        color: #1E88C7;
        text-decoration: none;
        text-indent: 0;
        line-height: 20px;
    }
    .file input {
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
    }
    .file:hover {
        background: #AADFFD;
        border-color: #78C3F3;
        color: #004974;
        text-decoration: none;
    }
    .my_btn
    {
        background: linear-gradient(bottom,blue,#fff);
        background: -ms-linear-gradient(bottom,blue,#fff);
        background: -webkit-linear-gradient(bottom,blue,#fff);
        background: -o-linear-gradient(bottom,blue,#fff);
        background: -moz-linear-gradient(bottom,blue,#fff);

        width:80%;
        height:2em;
        border-radius:25px;
        text-align: center;
        vertical-align: middle;
        margin-left: 10%;
        font-size:1.6em;
        z-index: 999;
    }
</style>
<script>
    var uploadFiles = new Array();
    $(function(){
        //阻止浏览器默认行为。
        $(document).on({
            dragleave:function(e){    //拖离
                e.preventDefault();
            },
            drop:function(e){  //拖后放
                e.preventDefault();
            },
            dragenter:function(e){    //拖进
                e.preventDefault();
            },
            dragover:function(e){    //拖来拖去
                e.preventDefault();
            }
        });
        var box = document.getElementById('dropbox'); //拖拽区域
        box.addEventListener("drop",function(e){
            e.preventDefault(); //取消默认浏览器拖拽效果
            var fileList = e.dataTransfer.files; //获取文件对象
            //检测是否是拖拽文件到页面的操作
            if(fileList.length == 0){
                return false;
            }
            AddFiles(fileList);
        },false);
    });
    function Refresh(){
        location.reload();
    }
    
    function Upload(){
        AddFiles(new Array());
        if(uploadFiles.length <= 0){
            Refresh();
            return;
        }
        uploadcount = uploadFiles.length ;
        var FileController = "UploadServlet";                    // 接收上传文件的后台地址
        // FormData 对象

        var form = new FormData();
        form.append("file", uploadFiles[0]);
        // XMLHttpRequest 对象
        var xhr = new XMLHttpRequest();
        xhr.open("post", FileController, true);
        xhr.onload = function () {
            Upload();
        };
        xhr.send(form);
        uploadFiles.splice(0,1);
    }
    function onc(){
        var files = document.getElementById("file").files;
        if(files.length < 0){
            return ;
        }
        AddFiles(files);
    }
    function AddFiles(files){
        var errstr = "";
        for(var i=0; i< files.length; i++){
            var filename = files[i].name;
            var isfind = false;
            for(var j=0; j< uploadFiles.length; j++){
                if(uploadFiles[j].name == filename){
                    isfind = true;
                    break;
                }
            }
            var index1=filename.lastIndexOf(".");
            var index2=filename.length;
            var postf=filename.substring(index1+1,index2);//后缀名
            //var myarray = new Array('JPG','jpg','jpeg','JPEG','gif','GIF','png','PNG','txt','xlsx','xls','doc','docx','rtf','csv');
            var myarray=new Array('xlsx','xlsx','csv');
            if($.inArray(postf,myarray) == -1){
                errstr += filename + "/";
                continue;
            }
            if(isfind == false){
                uploadFiles.push(files[i]);
            }
        }
        if(errstr != ""){
            alert("文件格式错误:"+errstr);
        }
        var fileliststring = "";
        if(uploadFiles.length<=0){
            $("#upload").hide();
        }
        else{$("#upload").show();}
        for(var j=0; j< uploadFiles.length; j++){
            fileliststring += "<tr><td align='center'>"+uploadFiles[j].name +"</td>"+"<td align='center'>大小:" + (uploadFiles[j].size / 1000) + "k" + "</td></tr>";
        }
        document.getElementById("fileliststring").innerHTML=fileliststring;
    }
</script>
<body>
<div name="dropbox" id="dropbox" class="divcss5" >
    <p style="text-align: center;margin-top: 20%">将文件拖拽至此区域<br>
        <br><span>-或-</span><br><br>
        <a style="width: 120px;text-align: center" href="javascript:void(0);" class="file">选择文件
            <input id="file"  class="filePrew" type="file" name="file" multiple="multiple" onchange="javascript:onc();" >
        </a>
    </p>
    <table id="fileliststring" class="layui-table" style="
        margin:20px auto;
        min-height: 0;
        width: 700px;
        height:auto; overflow:hidden;">
    </table>
    <button id="upload" class="layui-btn layui-btn-normal layui-btn-radius" style="display: none" type="submit" onclick="javascript:Upload();">上传文件</button>
</div>


</body>
</html>

2.实现对上传excel的读取

public class ExcelUtil {

    public String[][] readExcel(InputStream inputStream) throws IOException {

        FileInputStream fileInputStream=null;
        Workbook workbook=null;

        //fileInputStream=(FileInputStream) inputStream;
        workbook=new XSSFWorkbook(inputStream);

        Sheet sheet=workbook.getSheetAt(0);

        Row row_n=sheet.getRow(0);
        int colmon_num=row_n.getLastCellNum();

        String[][] key_value=new String[3][colmon_num];

        for (int i=0;i<sheet.getLastRowNum();i++)
        {
            Row row=sheet.getRow(i);
            for(int j=0;j<row.getLastCellNum();j++)
            {
                Cell cell=row.getCell(j);
                String value=cell.getStringCellValue();
                key_value[i][j]=value;
            }
        }

        return key_value;
    }

}

3.实现对读取结果的保存txt

public void insertText(String[][] key_value) throws IOException {
        String uploadPath = this.getServletContext().getRealPath("/src/info.txt");
        File uploadFile = new File(uploadPath);
        if (!uploadFile.exists()) {//如果目录不存在,创建这样一个目录;
            uploadFile.createNewFile();
        }


        BufferedWriter fileWriter = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (uploadPath,true),"UTF-8"));

        for(int colmon=0;colmon<key_value[0].length;colmon++)
        {
            String value=key_value[0][colmon]+","+key_value[1][colmon]+","+key_value[2][colmon];
            if(key_value[1][colmon]==""||key_value[1][colmon]==null)
            {
                value+=","+"varchar(255)"+"\n";
            }
            else
            {
                value+=","+"int"+"\n";
            }
            fileWriter.write(value);
        }
        fileWriter.flush();
        fileWriter.close();
    }

4.读取txt文件

public void readTxt_value(String path) throws IOException {
            String encoding="UTF-8";
        File file=new File(path);
        InputStreamReader inputStreamReader=new InputStreamReader(
                new FileInputStream(file),encoding
        );
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        String lineTxt=null;
        while ((lineTxt= bufferedReader.readLine())!=null)
        {
            System.out.println(lineTxt);
        }
        bufferedReader.close();
        inputStreamReader.close();

    }

 

posted @ 2021-10-30 09:56  新古董  阅读(41)  评论(0)    收藏  举报