如何将带有json的txt文件发送到后端并使用它

时间:2018-09-21 13:13:41

标签: javascript java json request

我正在寻找解决方案。 我有一个page.jsp,需要将txt文件发送到服务器,并使用Json和JavaScript进行读取。

有我的页面。

<form:form id="formFile" name="formFile" enctype="multipart/form-data" action="" method="post">
<div class="g1" style="width: 500px;">
<label for="" class="left">Select the file:</label>
<input type="file" id="fileID" name="fileID" class=""/>
</div>
<div class="g1">
<label for="" class="left"></label>
<a class="btn" id="exportFromFile" href="#" onclick="submitFile(this);">SubmitFile</a>
</div>
</form:form>

<script type="text/javascript">

function submitFile(btn){
var formData = new FormData(document.getElementById("fileID").files[0].name;
var file = '?fileID='+ document.getElementById("fileID").files[0].name;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
  --- validation with Json and alerts}

};
xmlhttp.open("POST","submitFile.do"+file);
xmlhttp.setResquestHeader("Content-type","multipart/form-data ; boundary=----boundary");
xmlhttp.send(formData);
document.getElementById("fileID").value="";
};
</script>

现在我的控制器中有

@RequestMapping(value="submitFile", method = RequestMethod.POST, produces = "aplication/json")
@ResponseBody
public NewSolic submitNewSolic(HttpServletRequest request, HttpServletResponse response)throws IOException{
        NewSolic validation = new NewSolic();
        String filename = request.getParameter("fileID");
        List<String> list = new ArrayList<String>();
         boolean isMultipart = ServetFileUpload.isMultipartContent(request);
        if(isMultipart){
               FileItemFactory factory = new DiskFileItemFactory():
               ServLetFileUpload upload = new ServletFileUpload(factory);
              try{
                    List<FileItem> items = upload.parseRequest(request);
                     Iterator<FileItem> iter = items.iterator();
                     StringBuffer stringValues = new StringBuffer();
                     while (iter.hasNext()){
                           FileItem item = (FileItem) iter.next();
                           if(item.getString() != null){
                           String[] itensUploadedArray = item.getString().split("\r\n");
                    for( String itemUploaded:itensUploadedArray){
                         list.add(itemUploaded);
                          -- use this list to a insert values after get all values in the txt--
                         }
                     }
                  }
           }

       }

问题出在“ while(iter.hasNext())”中,我认为内存中的文件不包含数据,并且不填充StringBuffer,该文件仅包含数字,但必须在被存储后才在数据库中阅读。

1 个答案:

答案 0 :(得分:0)

在链接上,您可以看到,如果要发送整个表单,则不必对文件进行任何魔术操作,只需选择整个表单,将其放入FormData,然后发送: / p>

function submitFile(btn){
  var form = document.forms.namedItem("formFile");
  var oData = new FormData(form);
  // you can add another fields in this way
  oData.append("CustomField", "This is some extra data");

  var oReq = new XMLHttpRequest();
  oReq.open("POST", "submitFile.do"+file);
  oReq.onload = function(oEvent) {
    if (oReq.status == 200) {
      alert("Uploaded!");
    } else {
      alert("Error " + oReq.status + " occurred when trying to upload your file.");
    }
  };

  oReq.send(oData);
 }

请尝试一下。