2014-12-24 90 views
0

我想要做這樣的事情。如何將多部分文件數據傳遞到Web服務

1.Uploading從jsp文件在多

2.Calling一個servlet

3.In的doPost這個servlet我想的調用webservice這需要由JSP沿Servlet進行傳遞的所有PARAMS與多部分數據。

dopost(javax.servlet.http.HttpServletRequest request, 
      javax.servlet.http.HttpServletResponse response){ 

    webserviceMethod(request,response); 

} 

我卡上的第三點,我可以將所有請求參數到webservice方法。但我不知道如何將多部分文件數據傳遞給websevice。我沒有這樣做。我怎樣才能做到這一點?

+1

請參閱[here](https://docs.oracle.com/javaee/7/tutorial/servlets011.htm#BABFGCHB) –

+0

謝謝peeskillet。但是沒有幫助我的要求。我的要求是我需要將多部分文件數據(文本/ XML/Excel等)傳遞到Web服務參數。 – Naveen

+0

你沒看過嗎?您可以從請求中獲取所有['javax.servlet.http.Part'](http://docs.oracle.com/javaee/6/api/javax/servlet/http/Part.html)。然後您可以從該部分獲得'InputStream',以及其他細節。您不會將它作爲'.txt'文件從流中傳入。您需要進行任何需要的轉換 –

回答

0

看一看是jQuery插件:

http://jquery.malsup.com/form/

我也是在我的應用程序一起使用這與一個Java servlet:

uploadImage: function (e) { 
     var self = this; 
     self.ignoreDrag(e); 
     if ($('#feed_imageUploader').find('input').hasClass('error')) { 
      return; 
     } 
     //cant put this in an model - ajaxSubmit has no done callback 
     $('#img_uploaded h1').text(polyglot.t('iview.loading')); 
     $('#feed_imageUploader').ajaxSubmit({ 
      target: '#img_uploaded', 
      type: "POST", 
      url: path.apiPath + 'item.uploadImg/' + self.itemId + '/' + token, 
      dataType: "text", 
      async: true, 
      success: function() { 
       self.afterUploadImage(); 
      } 
     }); 

    }, 
afterUploadImage: function() { 
     var self = this; 
     self.changed = true; 
     var xFactorItemImage = 0; 
     var yFactorItemImage = 0; 
     var randomnumber = Math.floor(Math.random() * 10100000); 
     $('#img_uploaded').html("<img src=\"" + path.tempImage + userId + "_" + self.itemId + ".jpg?id=" + randomnumber + "\" style=\"display:none\" id=\"cropPhoto_uploaded\">"); 
     var theImage = new Image(); 
     var cropPhoto = $('#cropPhoto_uploaded'); 
     theImage.src = cropPhoto.attr("src"); 
     var widthPhoto = 0; 
     var heightPhoto = 0; 
     var NwidthPhoto = 0; 
     var NheightPhoto = 0; 
     $(theImage).load(function() { 
      $('#img_uploaded h1').empty(); 
      $('#additemimage').hide(); 
      NwidthPhoto = theImage.width; 
      NheightPhoto = theImage.height; 
      cropPhoto.css({ 
       maxHeight: $('#img_uploaded').height() + 'px', 
       maxWidth: $('#img_uploaded').width() + 'px' 
      }); 
      cropPhoto.show(); 
      $('#addimage_upload').fadeIn(aSpeed.middle); 
      widthPhoto = cropPhoto.width(); 
      heightPhoto = cropPhoto.height(); 
      xFactorItemImage = NwidthPhoto/widthPhoto; 
      yFactorItemImage = NheightPhoto/heightPhoto; 
      cropPhoto.Jcrop({ 
       setSelect: helper.getMiddleSelectionOfImage(widthPhoto, heightPhoto, widthPhoto, heightPhoto), 
       bgOpacity: 0.3, 
       onChange: showItemImageCoords, 
       onSelect: showItemImageCoords 
      }); 
     }); 

     function showItemImageCoords(c) { 
      $('#x111').val(parseInt(xFactorItemImage * c.x)); 
      $('#y111').val(parseInt(yFactorItemImage * c.y)); 
      $('#x222').val(parseInt(xFactorItemImage * c.w)); 
      $('#y222').val(parseInt(yFactorItemImage * c.h)); 
     } 
    }, 

並且servlet部分:

public void UploadImage(HttpServletRequest request, String filename, String folder,String bucketname) { 
    File file; 
    PropertyReader mainconf = new PropertyReader(); 
    DiskFileItemFactory factory = new DiskFileItemFactory(); 
    ServletFileUpload upload = new ServletFileUpload(factory); 
    List items ; 
    mainconf.getProb("conf/MainConfig.properties"); 
    s3 s3=new s3(); 
    try { 
     items = upload.parseRequest(request); 
     // Process the uploaded file items 
     Iterator i = items.iterator(); 

     //Iterate through the items 
     String finalPath = ""; 
     FileItem fi; 
     while (i.hasNext()) { 
      fi = (FileItem) i.next(); 
      if (!fi.isFormField()) { 
       // Get the uploaded file parameters    
       String your_os = System.getProperty("os.name").toLowerCase(); 
       String workingDir = "images"; 
       finalPath = mainconf.read("imagePath"); 
       if (your_os.indexOf("win") >= 0) { 
        finalPath = finalPath + workingDir + "\\" + folder + "\\"; 
       } else if (your_os.indexOf("nix") >= 0 || your_os.indexOf("nux") >= 0) { 
        finalPath = finalPath + workingDir + "/" + folder + "/"; 
       } else { 
        finalPath = finalPath + workingDir + "{others}" + folder + "{others}"; 
       } 
       file = new File(finalPath + filename + ".jpg"); 
       fi.write(file); 
       s3.writeFile(bucketname, file, filename+".jpg"); 
       file.delete(); 

      } 
      break; 
     } 

    } catch (Exception ex) { 
     Logger.getLogger(UploadItemImage.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
+0

Fabian。在此先感謝。我發佈我的問題有點誤導。讓我說清楚。即時通訊從一個jsp上載文件(文本/ XML等)在多部分加密,從那裏即時調用一個servlet。在該servlet的dopost中,我需要將該多部分數據傳遞給web服務.i可以使用annotatin @FormDataParam將隱藏的參數添加到該web服務。我想要的是將多部分文件數據傳遞給Servlet dopost()中的webservice,因爲我即將在請求中獲取多部分文件數據。 – Naveen

+0

好的。也許這對別人來說也很有趣,你爲什麼要這樣做 - 也許有一個更簡單/更好的方法;) –

相關問題