使用java客户端上传Azure Web作业

使用java客户端上传Azure Web作业,java,azure,azure-webjobs,Java,Azure,Azure Webjobs,上下文的历史记录: 我正在尝试从HTTP客户端运行web作业。该文件是一个ZIP文件。并包含一个java类和运行该java类的bat文件。当我从邮递员那里得到的时候,这个运行正常。但是当我使用HTTP客户端时,总是会出现以下错误“'--i-NPsGbTVUpaP0CeJxMQVrHoDHvaxo3'未被识别为内部或外部命令”-请帮助-Jagaran @贾加兰:如果这只发生在一些客户身上,那很可能是无关的。请问一个新问题——21小时前的David Ebbo 没有任何我在java中使用的HTTP客户

上下文的历史记录: 我正在尝试从HTTP客户端运行web作业。该文件是一个ZIP文件。并包含一个java类和运行该java类的bat文件。当我从邮递员那里得到的时候,这个运行正常。但是当我使用HTTP客户端时,总是会出现以下错误“'--i-NPsGbTVUpaP0CeJxMQVrHoDHvaxo3'未被识别为内部或外部命令”-请帮助-Jagaran

@贾加兰:如果这只发生在一些客户身上,那很可能是无关的。请问一个新问题——21小时前的David Ebbo

没有任何我在java中使用的HTTP客户端,它是相同的。它可以在CURL中工作,也可以从web控制台加载。下面是我的示例代码–2小时前的Jagaran

没有任何我在java中使用的HTTP客户端,它是相同的。它可以在CURL中工作,也可以从web控制台加载

您是否有任何基于Java的HTTP客户端示例,我可以在其中发布Azure Web作业?我已经尝试了所有Java REST客户端

可能是我做错了什么。我在Azure控制台中遇到的错误是“--I-NPsGbTVUpaP0CeJxMQVrHoDHvaxo3”未被识别为内部或外部命令,[08/25/2017 09:30:22>e7f683:ERR]可操作的程序或批处理文件。o

我觉得在使用java时Content type=application/zip没有正确发生。请帮助我们。 示例代码:

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.http.entity.ContentType;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;


/**
 * @author jagaran.das
 *
 */
public class AIPHTTPClient {

    /**
     * @param args
     * @throws IOException 
     */
    @SuppressWarnings({ "unused", "rawtypes" })
    public static void main(String[] args) throws IOException {
        try {

            URI  uri = new AIPHTTPClient().getURI();
            HttpResponse<InputStream> jsonResponse = Unirest.put("https://<URL>/api/triggeredwebjobs/TestJOb")
                .basicAuth("$AzureWebJobTestBRMS", "XXXXX")
                    .header("content-disposition","attachement; filename=acvbgth.bat")
                    .field("file", new  FileInputStream(new File(uri)) 
                        ,ContentType.create("content-type: application/zip"),"AzureWebJob.zip").asBinary();
             System.out.println(jsonResponse.getStatusText()); 


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public InputStream readZip() {
        ZipFile zipFile = null;
        ZipEntry zipEntry = zipFile.getEntry("run.bat");

        InputStream stream = null;
    /*  try {
            zipFile = new ZipFile("/Users/jagaran.das/Documents/work/AIP/AzureWebJob.zip");
            java.util.Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while(entries.hasMoreElements()){
                ZipEntry entry = entries.nextElement();
                stream = zipFile.getInputStream(entry);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } */
        try {
            stream = zipFile.getInputStream(zipEntry);
        } catch (IOException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
        }
   return stream;

    }


  public URI getURI() throws MalformedURLException {
    File file = new File("/Users/jagaran.das/Documents/work/AIP/azure-poc/AzureWebJob.zip");

    URI fileUri = file.toURI();
    System.out.println("URI:" + fileUri);


    URL fileUrl = file.toURI().toURL();
    System.out.println("URL:" + fileUrl);

    URL fileUrlWithoutSpecialCharacterHandling = file.toURL();
    System.out.println("URL (no special character handling):" + fileUrlWithoutSpecialCharacterHandling);
    return fileUri;
       }

}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.net.MalformedURLException;
导入java.net.URI;
导入java.net.URL;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipFile;
导入org.apache.http.entity.ContentType;
导入com.mashape.unirest.http.HttpResponse;
导入com.mashape.unirest.http.unirest;
/**
*@author jagaran.das
*
*/
公共类AIPHTTPClient{
/**
*@param args
*@抛出异常
*/
@SuppressWarnings({“unused”,“rawtypes”})
公共静态void main(字符串[]args)引发IOException{
试一试{
URI URI=new AIPHTTPClient().getURI();
HttpResponse jsonResponse=Unirest.put(“https:///api/triggeredwebjobs/TestJOb")
.basicAuth($AzureWebJobTestBRMS,“$XXXXX”)
.header(“内容处置”、“附件;文件名=acvbgth.bat”)
.field(“文件”,新文件输入流(新文件(uri))
,ContentType.create(“内容类型:application/zip”),“AzureWebJob.zip”).asBinary();
System.out.println(jsonResponse.getStatusText());
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共输入流readZip(){
ZipFile ZipFile=null;
ZipEntry=zipFile.getEntry(“run.bat”);
InputStream=null;
/*试一试{
zipFile=newZipFile(“/Users/jagaran.das/Documents/work/AIP/AzureWebJob.zip”);

java.util.Enumeration在真正尝试之前,我的回答有点过于苛刻。抱歉。我现在已经尝试了您的代码片段,看起来您与Unirest之间可能有问题

我的建议是搬到。
以下是一个工作示例:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;

public class App 
{
    public static void main( String[] args )
    {
        File sourceZipFile = new File("webjob.zip");
        String kuduApiUrl = "https://yoursitename.scm.azurewebsites.net/api/zip/site/wwwroot/app_data/jobs/triggered/job988/";

        HttpEntity httpEntity = EntityBuilder.create()
        .setFile(sourceZipFile)
        .build();

        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            "$yoursitename", "SiteLevelPasSw0rD"
        );
        provider.setCredentials(AuthScope.ANY, credentials);

        HttpClient client = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(provider)
            .build();

        HttpPut putRequest = new HttpPut(kuduApiUrl);
        putRequest.setEntity(httpEntity);
        // Kudu's Zip API expects application/zip
        putRequest.setHeader("Content-type", "application/zip");

        try {
            HttpResponse response = client.execute(putRequest);
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            String resBody = EntityUtils.toString(entity, "UTF-8");
            System.out.println(statusCode);
            System.out.println(resBody);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
这发送了
内容类型:application/zip
和正文中的原始zip内容(没有多部分的马粪)。我可能过度设计了示例。但它就是这样

上载成功并发布了WebJob:


在真正尝试之前,我的回答有点太苛刻了。抱歉。我现在已经尝试了您的代码片段,看起来您与Unirest之间可能有问题

我的建议是搬到。
以下是一个工作示例:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;

public class App 
{
    public static void main( String[] args )
    {
        File sourceZipFile = new File("webjob.zip");
        String kuduApiUrl = "https://yoursitename.scm.azurewebsites.net/api/zip/site/wwwroot/app_data/jobs/triggered/job988/";

        HttpEntity httpEntity = EntityBuilder.create()
        .setFile(sourceZipFile)
        .build();

        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            "$yoursitename", "SiteLevelPasSw0rD"
        );
        provider.setCredentials(AuthScope.ANY, credentials);

        HttpClient client = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(provider)
            .build();

        HttpPut putRequest = new HttpPut(kuduApiUrl);
        putRequest.setEntity(httpEntity);
        // Kudu's Zip API expects application/zip
        putRequest.setHeader("Content-type", "application/zip");

        try {
            HttpResponse response = client.execute(putRequest);
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            String resBody = EntityUtils.toString(entity, "UTF-8");
            System.out.println(statusCode);
            System.out.println(resBody);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
这发送了
内容类型:application/zip
和正文中的原始zip内容(没有多部分的马粪)。我可能过度设计了示例。但它就是这样

上载成功并发布了WebJob:


很高兴您已经解决了这个问题,我尝试提供一个解决方法供您参考

将WebJob部署到azure,除了使用
RESTAPI
,您还可以使用
FTP
方式。当然,前提是您需要知道WebJob通过
KUDU
上传的目录

我通过FTP4J图书馆向您提供以下代码片段:

import java.io.File;
import it.sauronsoftware.ftp4j.FTPClient;

public class UploadFileByFTP {

    private static String hostName = <your host name>;
    private static String userName = <user name>;
    private static String password = <password>;

    public static void main(String[] args) {
        try {
            // create client
            FTPClient client = new FTPClient();
            // connect host
            client.connect(hostName);
            // log in
            client.login(userName, password);
            // print address
            System.out.println(client);

            // change directory
            client.changeDirectory("/site/wwwroot/App_Data/jobs/continuous");
            // current directory
            String dir = client.currentDirectory();
            System.out.println(dir);

            File file = new File("D:/test.zip");
            client.upload(file);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
导入java.io.File;
导入it.sauronsoftware.ftp4j.FTPClient;
公共类UploadFileByFTP{
私有静态字符串主机名=;
私有静态字符串userName=;
私有静态字符串密码=;
公共静态void main(字符串[]args){
试一试{
//创建客户端
FTPClient client=新的FTPClient();
//连接主机
client.connect(主机名);
//登录
client.login(用户名、密码);
//打印地址
System.out.println(客户端);
//更改目录
client.changeditory(“/site/wwwroot/App_Data/jobs/continuous”);
//当前目录
String dir=client.currentDirectory();
系统输出打印项次(dir);
File File=新文件(“D:/test.zip”);
上传(文件);
}捕获(例外e){
e、 printStackTrace();
}
}
}

您可以按照此操作来配置参数。

很高兴您已经解决了这个问题,我尝试提供一个解决方法供您参考

将WebJob部署到azure,除了使用
RESTAPI
,您还可以使用
FTP
方式。当然,前提是您需要知道WebJob通过
KUDU
上传的目录

我通过FTP4J图书馆向您提供以下代码片段:

import java.io.File;
import it.sauronsoftware.ftp4j.FTPClient;

public class UploadFileByFTP {

    private static String hostName = <your host name>;
    private static String userName = <user name>;
    private static String password = <password>;

    public static void main(String[] args) {
        try {
            // create client
            FTPClient client = new FTPClient();
            // connect host
            client.connect(hostName);
            // log in
            client.login(userName, password);
            // print address
            System.out.println(client);

            // change directory
            client.changeDirectory("/site/wwwroot/App_Data/jobs/continuous");
            // current directory
            String dir = client.currentDirectory();
            System.out.println(dir);

            File file = new File("D:/test.zip");
            client.upload(file);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
导入java.io.File;
导入it.sauronsoftware.ftp4j.FTPClient;
公共类UploadFileByFTP{
私有静态字符串主机名=;
私有静态字符串userName=;
私家侦探