2012-02-10 1030 views
2

我正在调用一个SOAP服务,它返回一个我保存的文件(请参阅下面的代码)。我想使用服务器发送给我的原始文件名来保存它。正如你所看到的,我只是硬编码保存流的文件名。Java:获取下载附件的文件名(HttpClient,PostMethod)

def payload = """ 
<SOAP-ENV:Body><mns1:getFile xmlns:mns1="http://connect.com/"> 
<userLogicalId>${params.userLogicalId}</userLogicalId> 
<clientLogicalId>${params.clientLogicalId}</clientLogicalId> 

def client = new HttpClient() 

def statusCode = client.executeMethod(method) 
InputStream handler = method.getResponseBodyAsStream() 

//TODO: The new File(... has filename hard coded). 
OutputStream outStr = new FileOutputStream(new File("c:\\var\\nfile.zip")) 

byte[] buf = new byte[1024] 
int len 
while ((len = handler.read(buf)) > 0) { 
    outStr.write(buf, 0, len); 
} 
handler.close(); 
outStr.close(); 

所以基本上,我想在响应中所获取的文件名。谢谢。

回答

2

在响应头,设置Content-Disposition"attachment; filename=\"" + fileName + "\""

+0

感谢两个答案

int index = dispositionValue.indexOf("filename="); if (index > 0) { filename = dispositionValue.substring(index + 10, dispositionValue.length() - 1); } System.out.println("Downloading file: " + filename); 

的完整代码中给出。问题在于SOAP服务没有在头中添加文件名。当我读取响应头时,我得到这个
,Content-Type:multipart/related;类型= “应​​用/ XOP + xml” 的;边界= “---- = _ Part_0_1546767.1329120288435”; start =“”; start-info =“text/xml” ,Set-Cookie:stage_80_evi = 2078807832.1.4098350016.1071872916;路径= / – ibaralf 2012-02-13 08:17:06

1

如果你有过发送文件的API控制,可以确保该API将适当content-disposition header。然后,在您代码中您收到文件的位置,您可以阅读内容处置标题并从中找到原始文件名。

这是从commons fileupload借用的代码,它从content-disposition头中读取文件名。

private String getFileName(String pContentDisposition) { 
     String fileName = null; 
     if (pContentDisposition != null) { 
      String cdl = pContentDisposition.toLowerCase(); 
      if (cdl.startsWith(FORM_DATA) || cdl.startsWith(ATTACHMENT)) { 
       ParameterParser parser = new ParameterParser(); 
       parser.setLowerCaseNames(true); 
       // Parameter parser can handle null input 
       Map params = parser.parse(pContentDisposition, ';'); 
       if (params.containsKey("filename")) { 
        fileName = (String) params.get("filename"); 
        if (fileName != null) { 
         fileName = fileName.trim(); 
        } else { 
         // Even if there is no value, the parameter is present, 
         // so we return an empty file name rather than no file 
         // name. 
         fileName = ""; 
        } 
       } 
      } 
     } 
     return fileName; 
    } 

您将需要阅读的内容 - disposition头,然后用它分裂“;”首先再用“=”分隔每个标记以获取名称值对。

0

您可以使用Content-Disposition Header来确定并保存。下面使用Apache HttpComponents http://hc.apache.org

public static void main(String[] args) throws ClientProtocolException, IOException { 

    CloseableHttpClient httpclient = HttpClients.createDefault(); 
    HttpGet httpGet = new HttpGet(
      "http://someurl.com"); 
    CloseableHttpResponse response = httpclient.execute(httpGet); 

    try { 
     System.out.println(response.getStatusLine()); 
     HttpEntity entity = response.getEntity(); 
     System.out.println("----------------------------------------"); 
     System.out.println(entity.getContentType()); 
     System.out.println(response.getFirstHeader("Content-Disposition").getValue()); 

     InputStream input = null; 
     OutputStream output = null; 
     byte[] buffer = new byte[1024]; 

     try { 
      String filename = "test.tif"; 
      String dispositionValue = response.getFirstHeader("Content-Disposition").getValue(); 
      int index = dispositionValue.indexOf("filename="); 
      if (index > 0) { 
       filename = dispositionValue.substring(index + 10, dispositionValue.length() - 1); 
      } 
      System.out.println("Downloading file: " + filename); 
      input = entity.getContent(); 
      String saveDir = "c:/temp/"; 

      output = new FileOutputStream(saveDir + filename); 
      for (int length; (length = input.read(buffer)) > 0;) { 
       output.write(buffer, 0, length); 
      } 
      System.out.println("File successfully downloaded!"); 
     } finally { 
      if (output != null) 
       try { 
        output.close(); 
       } catch (IOException logOrIgnore) { 
       } 
      if (input != null) 
       try { 
        input.close(); 
       } catch (IOException logOrIgnore) { 
       } 
     } 
     EntityUtils.consume(entity); 
    } finally { 
     response.close(); 
     System.out.println(executeTime); 
    } 
}