1. ホーム
  2. Reporting Errors

SyntaxError: JSON の位置 1 に予期しないトークン s があります。

2022-02-16 08:41:21

レンダリングです。

前回のbootstrap filuploadのアップロードで、このエラーに遭遇し、successを送るように間違って書かれていたことがわかったので、他のフィールドを追加せずにtrueとfalseを送ればよいことにし、アップロード成功後のコールバック関数に変更しました。

 //Import events after the file upload is complete
	    $("#txt_file").on("fileuploaded", function (event, data, previewId, index) {
	        $("#myModal_file_upload").modal("hide");
	        var data = data.jqXHR.responseJSON;
	        if (data == true) {
    	        toastr.success("Upload successful");
	        }
	        if (data == false) {
		        toastr.error("Upload failed, same data exists, please check data");
	        }
	    });

この後、限り{成功:真},{成功:偽}として送信された背景文字列もこのエラーが発生する前に、この側は、フォアグラウンドの要件を満たしていない後にフォアグラウンドjson解析する必要がありますので、このエラー、あなたはフォーマットの要件後にフォアグラウンド解析を知って、することができるように対応する変更、この添付のすべてのコードをアップロードする必要があります。

ソースコードとAPIのアドレスです。

bootstrap-fileinput のソースコードです。 https://github.com/kartik-v/bootstrap-fileinput

bootstrap-fileinput オンライン API。 http://plugins.krajee.com/file-input

bootstrap-fileinput デモのショーケースです。 http://plugins.krajee.com/file-basic-usage-demo


フロントエンドのjspです。

<link href="<%=basePath %>bootstrap/css/fileinput.min.css" rel="stylesheet">
<script type="text/javascript" src="<%=basePath %>bootstrap/js/fileinput.min.js" charset="UTF-8" ;></script>
    <script type="text/javascript" src="<%=basePath %>bootstrap/js/fileinput.zh-CN.js" charset="UTF-8& quot;></script>
<button id="button_imp" class="btn btn-sm btn-primary">Import device</button>
<form>
        <div class="modal fade" id="myModal_file_upload" tabindex="-1" role="dialog" aria-labelledby= "myModalLabel">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria -hidden="true">×</span></button>
                        <h4 class="modal-title" id="myModalLabel">Please select Excel file</h4>
                    </div>
                    <div class="modal-body">
<! -- <a href="~" class="form-control" style="border:none;">Download Import Template</a> -->
                             <h4 class="modal-title" id="myModalLabel">Importing devices</h4>
                        <input type="file" name="txt_file" id="txt_file" multiple class="file-loading" />
                        <! -- <input type="file" /> -->
                    </div>
                </div>
            </div>
        </div>
    </form>

jQuery(function($) {
	var oFileInput = new FileInput();
    oFileInput.Init("txt_file", "uploadExcel.action");
/** Import popup */
    $("#button_imp").click(function(event) {
        /* Act on the event */
        $("#myModal_file_upload").modal();
    });
}
//Initialize fileinput
	var FileInput = function () {
	    var oFile = new Object();

	    //initialize the fileinput control (first initialization)
	    oFile.Init = function(ctrlName, uploadUrl) {
	    var control = $('#' + ctrlName);

	    //Initialize the style of the upload control
	    control.fileinput({
	        language: 'zh', //set language
	        uploadUrl: uploadUrl, //address to upload
	        allowedFileExtensions: ['xlsx', 'xls','png'], //Received file extensions
	        showUpload: true, //whether to show the upload button
	        showCaption: false,//whether to show the title
	        browseClass: "btn btn-primary", //button style
	        //dropZoneEnabled: false,//whether to show the drag and drop area
	        //minImageWidth: 50, //minimum width of the image
	        //minImageHeight: 50,//minimum height of the image
	        //maxImageWidth: 1000,//maximum width of the image
	        //maxImageHeight: 1000,//maximum height of the image
	        //maxFileSize: 0,//Unit is kb, if it is 0, it means no file size limit
	        //minFileCount: 0,
	        maxFileCount: 1, //indicates the maximum number of files allowed to be uploaded at the same time
	        enctype: 'multipart/form-data',
	        validateInitialCount: true,
	        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
	        msgFilesTooMany: "The number of files selected for upload ({n}) exceeds the maximum allowed value of {m}! ",
	    })
	    //Import events after the file upload is complete
	    $("#txt_file").on("fileuploaded", function (event, data, previewId, index) {
	        $("#myModal_file_upload").modal("hide");
	        var data = data.jqXHR.responseJSON;
	        if (data == true) {
    	        toastr.success("Upload successful");
	        }
	        if (data == false) {
		        toastr.error("Upload failed, same data exists, please check data");
	        }
	    });
	}
	    return oFile;
	};



	public String upload() throws FileNotFoundException, IOException {
        HttpServletRequest req = ServletActionContext.getRequest();
        MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) req;
        File[] fileNames = wrapper.getFiles("txt_file");
        InputStream is = new FileInputStream(fileNames[0]);
        String[] filenames = wrapper.getFileNames("txt_file");
        String name = filenames[0];
        EquipmentInfoService equipmentInfoService=(EquipmentInfoService)SpringUtil.getObject("equipmentInfoService");
        EquipmentInfo equipmentInfoExa=new EquipmentInfo();
        Page page=new Page();
        List<EquipmentInfo> equipmentInfoLists=equipmentInfoService.findEquipmentInfo(equipmentInfoExa, page);
        
        OK:
        if (name.toUpperCase().endsWith(".XLS")
                || name.toUpperCase().endsWith(".XLSX")) {
            List<String[]> datas = null;
            ExcelSheetParser parser = new ExcelSheetParser(is, name);
            for (int n = 0; n < parser.getNumberOfSheets(); n++) {
                if (name.toUpperCase().endsWith(".XLS")) {
                    datas = parser.getDatasInSheet2003(n);
                } else if (name.toUpperCase().endsWith(".XLSX")) {
                    datas = parser.getDatasInSheet2007(n);
                }
                if (n == 0) {// Device information
                    for(int i = 1; i < datas.size(); i++){
                        String[] row1 = datas.get(i);
                        boolean flag=false;
                        for(EquipmentInfo equipmentInfo:equipmentInfoLists){
                            if(equipmentInfo.getCompanyId().equals(row1[0])){
                                if(equipmentInfo.getDeviceIP().equals(row1[6])){
                                    flag=true;
                                }
                            }
                        }
                        
                        if(flag){
                            this.setJsonStr("false");
                            break OK ;
                        }
                    }
                    for (int i = 1; i < datas.size(); i++) {// show data
                        String[] row = datas.get(i);
                        EquipmentInfo equipmentInfo = new EquipmentInfo();
                        for (int a = 0; a < row.length; a++) {
                            switch (a) {
                            case 0:
                                equipmentInfo.setCompanyId(row[0]);
                                break;
                            case 1:
                                equipmentInfo.setCompanyName(row[1]);
                                break;
                            case 2:
                                equipmentInfo.setMachineRoom(row[2]);
                                break;
                            case 3:
                                equipmentInfo.setEquipmentCabinet(row[3]);
                                break;
                            case 4:
                                equipmentInfo.setDeviceType(row[4]);
                                break;
                            case 5:
                                equipmentInfo.setDeviceName(row[5]);
                                break;
                            case 6:
                                equipmentInfo.setDeviceIP(row[6]);
                                break;
     
                                harddiskInfo.setCapacity(row[4]);
                                break;
                            case 5:
                                harddiskInfo.setSpeed(row[5]);
                                break;
                            case 6:
                                harddiskInfo.setInterface(row[6]);
                                break;
                            case 7:
                                harddiskInfo.setDeviceSN(row[7]);
                                break;
                            case 8:
                                harddiskInfo.setNewUpdatetime(row[8]);
                                break;
                            case 9:
                                harddiskInfo.setDevicePN(row[9]);
                                break;
                            case 10:
                                harddiskInfo.setRemarks(row[10]);
                                break;
                            }
                        }

                        service.addHarddiskInfo(harddiskInfo);
                    }
                }
                if (n == 4) {// power
                    for (int i = 1; i < datas.size(); i++) {// Display data
                        String[] row = datas.get(i);

                        PowerInfoEc powerInfoEc = new PowerInfoEc();

                        for (int a = 0; a < row.length; a++) {
                            switch (a) {
                            case 0:
                                powerInfoEc.setDeviceIP(row[0]);
                                break;
                            case 1:
                                powerInfoEc.setDeviceName(row[1]);
                                break;
                            case 2:
                                powerInfoEc.setDeviceBrand(row[2]);
                                break;
                            case 3:
                                powerInfoEc.setDeviceModel(row[3]);
                                break;
                            case 4:
                                powerInfoEc.setRatedVoltage(row[4]);
                                break;
                            case 5:
                                powerInfoEc.setDeviceSN(row[5]);
                                break;
                            case 6:
                                powerInfoEc.setNewUpdatetime(row[6]);
                                break;
                            case 7:
                                powerInfoEc.setRemarks(row[7]);
                                break;
                            }
                        }

                        service.addPowerInfoEc(powerInfoEc);
                    }
                }
                if (n == 5) {// fan
                    for (int i = 1; i < datas.size(); i++) {// Display data
                        String[] row = datas.get(i);

                        FanInfoEc fanInfoEc = new FanInfoEc();

                        for (int a = 0; a < row.length; a++) {
                            switch (a) {
                            case 0:
                                fanInfoEc.setDeviceIP(row[0]);
                                break;
                            case 1:
                                fanInfoEc.setDeviceName(row[1]);
                                break;
                            case 2:
                                fanInfoEc.setDeviceBrand(row[2]);
                                break;
                            case 3:
                                fanInfoEc.setDeviceModel

<form>
        <div class="modal fade" id="myModal_file_upload" tabindex="-1" role="dialog" aria-labelledby= "myModalLabel">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria -hidden="true">×</span></button>
                        <h4 class="modal-title" id="myModalLabel">Please select Excel file</h4>
                    </div>
                    <div class="modal-body">
<! -- <a href="~" class="form-control" style="border:none;">Download Import Template</a> -->
                             <h4 class="modal-title" id="myModalLabel">Importing devices</h4>
                        <input type="file" name="txt_file" id="txt_file" multiple class="file-loading" />
                        <! -- <input type="file" /> -->
                    </div>
                </div>
            </div>
        </div>
    </form>

フロントエンドのjsです。

jQuery(function($) {
	var oFileInput = new FileInput();
    oFileInput.Init("txt_file", "uploadExcel.action");
/** Import popup */
    $("#button_imp").click(function(event) {
        /* Act on the event */
        $("#myModal_file_upload").modal();
    });
}

//Initialize fileinput
	var FileInput = function () {
	    var oFile = new Object();

	    //initialize the fileinput control (first initialization)
	    oFile.Init = function(ctrlName, uploadUrl) {
	    var control = $('#' + ctrlName);

	    //Initialize the style of the upload control
	    control.fileinput({
	        language: 'zh', //set language
	        uploadUrl: uploadUrl, //address to upload
	        allowedFileExtensions: ['xlsx', 'xls','png'], //Received file extensions
	        showUpload: true, //whether to show the upload button
	        showCaption: false,//whether to show the title
	        browseClass: "btn btn-primary", //button style
	        //dropZoneEnabled: false,//whether to show the drag and drop area
	        //minImageWidth: 50, //minimum width of the image
	        //minImageHeight: 50,//minimum height of the image
	        //maxImageWidth: 1000,//maximum width of the image
	        //maxImageHeight: 1000,//maximum height of the image
	        //maxFileSize: 0,//Unit is kb, if it is 0, it means no file size limit
	        //minFileCount: 0,
	        maxFileCount: 1, //indicates the maximum number of files allowed to be uploaded at the same time
	        enctype: 'multipart/form-data',
	        validateInitialCount: true,
	        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
	        msgFilesTooMany: "The number of files selected for upload ({n}) exceeds the maximum allowed value of {m}! ",
	    })
	    //Import events after the file upload is complete
	    $("#txt_file").on("fileuploaded", function (event, data, previewId, index) {
	        $("#myModal_file_upload").modal("hide");
	        var data = data.jqXHR.responseJSON;
	        if (data == true) {
    	        toastr.success("Upload successful");
	        }
	        if (data == false) {
		        toastr.error("Upload failed, same data exists, please check data");
	        }
	    });
	}
	    return oFile;
	};



このバックエンドはjavaで、コードを掲載します。

	public String upload() throws FileNotFoundException, IOException {
        HttpServletRequest req = ServletActionContext.getRequest();
        MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) req;
        File[] fileNames = wrapper.getFiles("txt_file");
        InputStream is = new FileInputStream(fileNames[0]);
        String[] filenames = wrapper.getFileNames("txt_file");
        String name = filenames[0];
        EquipmentInfoService equipmentInfoService=(EquipmentInfoService)SpringUtil.getObject("equipmentInfoService");
        EquipmentInfo equipmentInfoExa=new EquipmentInfo();
        Page page=new Page();
        List<EquipmentInfo> equipmentInfoLists=equipmentInfoService.findEquipmentInfo(equipmentInfoExa, page);
        
        OK:
        if (name.toUpperCase().endsWith(".XLS")
                || name.toUpperCase().endsWith(".XLSX")) {
            List<String[]> datas = null;
            ExcelSheetParser parser = new ExcelSheetParser(is, name);
            for (int n = 0; n < parser.getNumberOfSheets(); n++) {
                if (name.toUpperCase().endsWith(".XLS")) {
                    datas = parser.getDatasInSheet2003(n);
                } else if (name.toUpperCase().endsWith(".XLSX")) {
                    datas = parser.getDatasInSheet2007(n);
                }
                if (n == 0) {// Device information
                    for(int i = 1; i < datas.size(); i++){
                        String[] row1 = datas.get(i);
                        boolean flag=false;
                        for(EquipmentInfo equipmentInfo:equipmentInfoLists){
                            if(equipmentInfo.getCompanyId().equals(row1[0])){
                                if(equipmentInfo.getDeviceIP().equals(row1[6])){
                                    flag=true;
                                }
                            }
                        }
                        
                        if(flag){
                            this.setJsonStr("false");
                            break OK ;
                        }
                    }
                    for (int i = 1; i < datas.size(); i++) {// show data
                        String[] row = datas.get(i);
                        EquipmentInfo equipmentInfo = new EquipmentInfo();
                        for (int a = 0; a < row.length; a++) {
                            switch (a) {
                            case 0:
                                equipmentInfo.setCompanyId(row[0]);
                                break;
                            case 1:
                                equipmentInfo.setCompanyName(row[1]);
                                break;
                            case 2:
                                equipmentInfo.setMachineRoom(row[2]);
                                break;
                            case 3:
                                equipmentInfo.setEquipmentCabinet(row[3]);
                                break;
                            case 4:
                                equipmentInfo.setDeviceType(row[4]);
                                break;
                            case 5:
                                equipmentInfo.setDeviceName(row[5]);
                                break;
                            case 6:
                                equipmentInfo.setDeviceIP(row[6]);
                                break;
     
                                harddiskInfo.setCapacity(row[4]);
                                break;
                            case 5:
                                harddiskInfo.setSpeed(row[5]);
                                break;
                            case 6:
                                harddiskInfo.setInterface(row[6]);
                                break;
                            case 7:
                                harddiskInfo.setDeviceSN(row[7]);
                                break;
                            case 8:
                                harddiskInfo.setNewUpdatetime(row[8]);
                                break;
                            case 9:
                                harddiskInfo.setDevicePN(row[9]);
                                break;
                            case 10:
                                harddiskInfo.setRemarks(row[10]);
                                break;
                            }
                        }

                        service.addHarddiskInfo(harddiskInfo);
                    }
                }
                if (n == 4) {// power
                    for (int i = 1; i < datas.size(); i++) {// Display data
                        String[] row = datas.get(i);

                        PowerInfoEc powerInfoEc = new PowerInfoEc();

                        for (int a = 0; a < row.length; a++) {
                            switch (a) {
                            case 0:
                                powerInfoEc.setDeviceIP(row[0]);
                                break;
                            case 1:
                                powerInfoEc.setDeviceName(row[1]);
                                break;
                            case 2:
                                powerInfoEc.setDeviceBrand(row[2]);
                                break;
                            case 3:
                                powerInfoEc.setDeviceModel(row[3]);
                                break;
                            case 4:
                                powerInfoEc.setRatedVoltage(row[4]);
                                break;
                            case 5:
                                powerInfoEc.setDeviceSN(row[5]);
                                break;
                            case 6:
                                powerInfoEc.setNewUpdatetime(row[6]);
                                break;
                            case 7:
                                powerInfoEc.setRemarks(row[7]);
                                break;
                            }
                        }

                        service.addPowerInfoEc(powerInfoEc);
                    }
                }
                if (n == 5) {// fan
                    for (int i = 1; i < datas.size(); i++) {// Display data
                        String[] row = datas.get(i);

                        FanInfoEc fanInfoEc = new FanInfoEc();

                        for (int a = 0; a < row.length; a++) {
                            switch (a) {
                            case 0:
                                fanInfoEc.setDeviceIP(row[0]);
                                break;
                            case 1:
                                fanInfoEc.setDeviceName(row[1]);
                                break;
                            case 2:
                                fanInfoEc.setDeviceBrand(row[2]);
                                break;
                            case 3:
                                fanInfoEc.setDeviceModel

この記事は、エクセルファイルとしてもアップロードされています。