在提交表单之前无法从jsp中的上传文件中获取值

时间:2014-05-19 05:05:29

标签: jsp

这是我用于捕获值的html代码:

 <input type="file" id="datafile" name="datafile" size="40" />
<input type="button" id="cmdUpload" value="Upload" />

这是我用来从文件标签中获取值的代码:

            boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
             response.setContentType("text/html");  
              PrintWriter priout = response.getWriter();  

              System.out.println("In fileupload servlet");

            if (isMultipart) {  
               // Create a factory for disk-based file items  
               FileItemFactory factory = new DiskFileItemFactory();  
               // Create a new file upload handler  
                System.out.println("Inside if");
               ServletFileUpload upload = new ServletFileUpload(factory);  
              try {  
                 // Parse the request  
                  System.out.println("Inside try");
                 List  items = upload.parseRequest(request);  
                Iterator iterator = items.iterator();  
                while (iterator.hasNext()) {  
                  FileItem item = (FileItem) iterator.next();  
                  if (!item.isFormField())  
                   {  
                    String fileName = item.getName();     
                    System.out.println("Inside if");
                    String root = getServletContext().getRealPath("/");  
                    File path = new File(root + "/uploads");  
                    if (!path.exists())  
                     {  
                      boolean status = path.mkdirs();  
                    }  
                    File uploadedFile = new File(path + "/" + fileName);  
                    System.out.println(uploadedFile.getAbsolutePath());  
                    System.out.println(uploadedFile.getTotalSpace());
                //    bytesray = uploadedFile.length();
                    byte[] b = new byte[(int)uploadedFile.length()];
                    for (int i = 0; i < b.length; i++) {
                   // System.out.print(b[i]);
                    }
                    String str = b.toString();
                    System.out.println(" byte array in string---"+str);
                    out.println("<h1>"+str+"</h1>");
                     if(fileName!="")  
                    item.write(uploadedFile);  
                     else  
                       out.println("file not found");  
                     out.println("<h1>File Uploaded Successfully....:-)</h1>");  
                  }  
                   else  
                   {  
                     String abc = item.getString();  
                     out.println("<br><br><h1>"+abc+"</h1><br><br>");  
                   }  
                }  
              } catch (FileUploadException e) {  
                    out.println(e);  
              } catch (Exception e) {  
                    out.println(e);  
              }  
            }  
             else  
             {  
               out.println("Not Multipart");  
             } 

我能够通过jquery获取价值,我可以从jquery或文件标签中获取价值。这是我使用的jquery:

 $(document).ready(function () {
   $('#cmdUpload').click(function () {
    alert("upload a file");
    uploadFile();

     });

   });

    function uploadFile() {
     var input = document.getElementById('datafile');
  alert("upload a file value is---"+input);
    // var file = $("#objFile")[0].files[0];
      var file = input.files[0];
     fr = new FileReader();
     fr.onload = receivedText;
    //fr.readAsText(file);
     fr.readAsDataURL(file);

       }

 function receivedText() {
         var b64Data = fr.result.split(',');
         var contentType = 'image/jpeg';
   //document.getElementById('editor').appendChild(document.createTextNode(fr.result))
     var byteCharacters = atob(b64Data[1]);
     var byteNumbers = Array.prototype.map.call(byteCharacters,
                               charCodeFromCharacter);
     var uint8Data = new Uint8Array(byteNumbers);
     var blob = b64toBlob(b64Data[1], contentType);
     var blobUrl = URL.createObjectURL(blob);
    }

   function charCodeFromCharacter(c) {
      return c.charCodeAt(0);
      }

      function b64toBlob(b64Data, contentType, sliceSize) {
           contentType = contentType || '';
           sliceSize = sliceSize || 1024;
           var byteCharacters = atob(b64Data);
           var byteArrays = [];

          for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
          var slice = byteCharacters.slice(offset, offset + sliceSize);
            var byteNumbers = Array.prototype.map.call(slice, charCodeFromCharacter);
             var byteArray = new Uint8Array(byteNumbers);
          byteArrays.push(byteArray);
        // $.post("fingerPrint.jsp"{'byte[]':[byteArray]});
        }
          for (var i = 0; i < byteArray.length; i++) {
          alert("byteArray values---"+byteArray[i]);
          }



      }  

任何人都可以帮助我让它发挥作用

0 个答案:

没有答案