Я пытаюсь загрузить файл из Restuous URI со следующим кодом:
public class JavaNetURLRESTFulClient {
private final String USER_AGENT = "Mozilla/5.0";
private Map<String,String> MimeTypes;
private URL restServiceURL;
private String authEncoded = null;
public JavaNetURLRESTFulClient (String URL) throws MalformedURLException {
restServiceURL = new URL(URL);
MimeTypes = new HashMap<String,String>();
MimeTypes.put("XLS","application/vnd.ms-excel");
}
public void setAuthetication (String user, String password) throws UnsupportedEncodingException {
String authString = user + ":" + password;
authEncoded = DatatypeConverter.printBase64Binary(authString.getBytes());
}
public void download (String fileType, String path) throws IOException {
HttpURLConnection httpConnection;
System.setProperty("http.agent", "");
httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestProperty("Accept-Charset", "UTF-8");
httpConnection.setRequestProperty("User-Agent", USER_AGENT);
httpConnection.setRequestProperty("Accept", MimeTypes.get(fileType));
if (authEncoded!=null){
httpConnection.setRequestProperty("Authorization", "Basic " + authEncoded);
httpConnection.setRequestProperty("Proxy-Authorization", "Basic " + authEncoded);
}
httpConnection.setRequestMethod("GET");
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
}
...
}
}
Я вызываю этот метод, делая:
url = args[0];
String[] schemes = {"http","https"};
UrlValidator urlValidator = new UrlValidator(schemes);
if (!urlValidator.isValid(url)) {
System.out.println("<Url> must be a valid URI");
return;
}
JavaNetURLRESTFulClient jrc = new JavaNetURLRESTFulClient (url);
jrc.setAuthetication ("user", "password");
class1.download ("XLS","c:\tmp.xls")
Когда я запускаю этот код из Eclipse, передавая параметр URL через Run → Run Configurations → Arguments
, он отлично работает. Но когда я запускаю этот код из Batch файла Windows (.BAT), он выдает ошибку Http 404:
java -jar download.jar "https://jira.< domain >/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+BSC+AND+issuetype+in+%28%22Big+Feature+Request%22%2C+Corrective%2C+%22Feature+Request%22%2C+%22Functional+Support%22%29+AND+created+%3E%3D+%222014-01-01+00%3A00%22+AND+created+%3C%3D+%222014-05-31+00%3A00%22+ORDER+BY+priority+DESC&tempMax=1000"
Может ли кто-нибудь знать, почему код работает из Eclipse, но не из командной строки?
измените командную строку на это:
java -jar download.jar "https://jira.< domain >/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%%3D+BSC+AND+issuetype+in+%%28%%22Big+Feature+Request%%22%%2C+Corrective%%2C+%%22Feature+Request%%22%%2C+%%22Functional+Support%%22%%29+AND+created+%%3E%%3D+%%222014-01-01+00%%3A00%%22+AND+created+%%3C%%3D+%%222014-05-31+00%%3A00%%22+ORDER+BY+priority+DESC&tempMax=1000"
вам нужно избежать %
с помощью другого%, чтобы предотвратить расширение существующей переменной.