网站文件上传为null

时间:2018-09-03 02:16:02

标签: java file jsp web file-upload

我正在尝试使用JSP页面上传文件。当我不使用任何精美的CSS表单组等代码时,该代码有效,但是当我使用以下样式和脚本时,我无法上传文件。

这是我的代码:

<%@page import="java.sql.*" %>
<%@page import="java.io.*"%>
<script>
  $(window).load(function() {
    $(".se-pre-con").fadeOut("slow");;
  });
</script>

<script>
function bs_input_file() {
    $(".input-file").before(
        function() {
            if ( ! $(this).prev().hasClass('input-ghost') ) {
                var element = $("<input type='file' class='input-ghost' style='visibility:hidden; height:0'>");
                element.attr("name",$(this).attr("name"));
                element.change(function(){
                    element.next(element).find('input').val((element.val()).split('\\').pop());
                });
                $(this).find("button.btn-choose").click(function(){
                    element.click();
                });
                $(this).find("button.btn-reset").click(function(){
                    element.val(null);
                    $(this).parents(".input-file").find('input').val('');
                });
                $(this).find('input').css("cursor","pointer");
                $(this).find('input').mousedown(function() {
                    $(this).parents('.input-file').prev().click();
                    return false;
                });
                return element;
            }
        }
    );
}
$(function() {
    bs_input_file();
});
</script>


<link href="date/bootstrap-datetimepicker.css" rel="stylesheet">
<link href="dataTable/css/bootstrap.css" rel="stylesheet">

<script type="text/javascript" src="js/bootstrap.min.js"></script>

<table width="100%" border="0" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
    <tr height = '50'>
        <td class="title1grey">E-Transport Batch Redeem Upload</td>
    </tr>
    <tr height = '20'><td></td></tr>
</table>   

<table style="width: 10%" class="table table-condensed borderless">

    <div class="col-md-3">
        <h5>Please Upload Batch Redeem</h5>
    <form name ="RedeemUpload" action="E-TickettingBatchRedeemUpload.jsp" method="POST" enctype="multipart/form-data" >
    <%

        String saveFile = new String();
        String contentType = request.getContentType();

        if((contentType != null) && (contentType.indexOf("multipart/form-data") >=0))
        {
            DataInputStream in = new DataInputStream(request.getInputStream());
            int formDataLenght =  request.getContentLength();
            byte dataBytes[] = new byte[formDataLenght];
            int byteRead = 0;
            int totalBytesRead = 0;

            while(totalBytesRead< formDataLenght){

                byteRead = in.read(dataBytes,totalBytesRead,formDataLenght);
                totalBytesRead += byteRead;     
            }       

            String file = new String(dataBytes);
            saveFile = file.substring(file.indexOf("filename=\"") +10);
            saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\") +1, saveFile.indexOf("\""));

            int lastIndex = contentType.lastIndexOf("=");
            String boundary = contentType.substring(lastIndex + 1, contentType.length());
            int pos;

            pos = file.indexOf("filename=\"");

            pos = file.indexOf("filename=\"");
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;

            int boundaryLocation = file.indexOf(boundary, pos) -4;
            int startPos = ((file.substring(0, pos)).getBytes()).length;
            int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; 

            saveFile = "C:/guru/upload/"+saveFile;

            File ff = new File(saveFile);

            try{

                FileOutputStream fileout = new FileOutputStream(ff);
                fileout.write(dataBytes,startPos, (endPos-startPos));
                fileout.flush();
                fileout.close();

            }
            catch(Exception ex){
                out.println(ex);
            }                    
        }
        else
        {
            out.println("Couldnt Detect a File");
        }


    %>
        <!-- COMPONENT START -->
        <div class="form-group">
            <div class="input-group input-file" name="Fichier1">
                <span class="input-group-btn">
                     <button class="btn btn-warning btn-reset" type="button">Reset</button>
                </span>
                <input type="text" class="form-control" placeholder='Choose a file...' value="text" />
                <span class="input-group-btn">
                    <button class="btn btn-default btn-choose" type="button">Choose</button>
                </span>             
            </div>
        </div>
        <!-- COMPONENT END -->
        <div class="form-group">            
            <input type="Submit" class="btn btn-primary" name="Upload" value="submit" />
        </div>      
    </form>
    </div>      
</table>

我确实想保留样式,因此请帮助调试。 当我将输入仅用作File时,文件成功通过,但是,当此JSP页面包含脚本时,就会出现问题。

0 个答案:

没有答案