我使用ASP.NET Web服务调用Java Web Service。 Java Web Service执行用户在URL中指示的批处理文件(例如http://localhost:8080/runbatchfile/test.bat
)。
ASP.NET Web服务充当API,在ASP.NET Web服务运行时键入URL http://localhost:62198/api/runbatchfile/test.bat
时应调用Java Web服务并返回Java Web服务返回的数据。
但是,我无法使用ASP.NET Web Service检索和显示数据,我觉得这是由.bat扩展名引起的。当我调用没有参数的Java Web服务或者只包含数字的参数时,这个ASP.NET Web服务可以工作但是当涉及扩展时我无法得到结果。
如果批处理文件被执行,我应该获得的结果是{"Result": true}
,如果没有执行批处理文件,我将获得{"Result": false}
。但是,我得到一个空的{}
。但是,当我运行它时,Java Web Service会正确显示结果。只有ASP.NET Web服务无法从Java Web服务读取数据并显示它。
我应该添加哪些代码以便包含.bat扩展名?请有人帮我提前谢谢你。
这是我到目前为止所做的:
JAVA CODE
BatchFileController.java
@RequestMapping("/runbatchfile/{param:.+}")
public ResultFormat runbatchFile(@PathVariable("param") String fileName) {
RunBatchFile rbf = new RunBatchFile();
return rbf.runBatch(fileName);
}
ResultFormat.java
private boolean result;
public ResultFormat(boolean result) {
this.result = result;
}
public boolean getResult() {
return result;
}
RunBatchFile.java
public ResultFormat runBatch(String fileName) {
String var = fileName;
String filePath = ("C:/Users/attsuap1/Desktop/" + var);
try {
Process p = Runtime.getRuntime().exec(filePath);
int exitVal = p.waitFor();
return new ResultFormat(exitVal == 0);
} catch (Exception e) {
e.printStackTrace();
return new ResultFormat(false);
}
ASP.NET代码
TestController.cs
private TestClient testClient = new TestClient();
public async Task<IHttpActionResult> GET(string fileName)
{
try
{
var result = await testClient.runbatchfile(fileName);
var resultDTO = JsonConvert.DeserializeObject<TestVariable>(result);
return Json(resultDTO);
}
catch (Exception e)
{
var result = "Server is not running";
return Ok(new { ErrorMessage = result });
}
}
TestVariable.cs
public class TestVariable
{
public static int fileName { get; set; }
}
TestClient.cs
public class TestClient
{
private static HttpClient client;
private static string BASE_URL = "http://localhost:8080/";
static TestClient()
{
client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<string> runbatchfile(string fileName)
{
var endpoint = string.Format("runbatchfile/{0}", fileName);
var response = await client.GetAsync(endpoint);
return await response.Content.ReadAsStringAsync();
}
}
答案 0 :(得分:2)
在URL中添加“.bat”扩展名可能不是最佳方法。 Web服务器无法识别此扩展,您需要进行一些调整以允许它,也可能导致一些安全问题无法控制,因为.bat文件被视为可执行文件。
事实上,如果您知道该文件始终是“.bat”文件,那么您不能完全省略URL中的扩展名,并且具有以下内容:
http://localhost:62198/api/runbatchfile/test
然后你只需在代码中添加它:
JAVA CODES
<强> BatchFileController.java 强>
@RequestMapping("/runbatchfile/{param:.+}")
public ResultFormat runbatchFile(@PathVariable("param") String fileName) {
RunBatchFile rbf = new RunBatchFile();
return rbf.runBatch(fileName);
}
<强> ResultFormat.java 强>
private boolean result;
public ResultFormat(boolean result) {
this.result = result;
}
public boolean getResult() {
return result;
}
<强> RunBatchFile.java 强>
public ResultFormat runBatch(String fileName) {
String var = fileName + ".bat";
String filePath = ("C:/Users/attsuap1/Desktop/" + var);
try {
Process p = Runtime.getRuntime().exec(filePath);
int exitVal = p.waitFor();
return new ResultFormat(exitVal == 0);
} catch (Exception e) {
e.printStackTrace();
return new ResultFormat(false);
}
ASP.NET代码
<强> TestController.cs 强>
private TestClient testClient = new TestClient();
public async Task<IHttpActionResult> GET(string fileName)
{
try
{
var result = await testClient.runbatchfile(Path.GetFileNameWithoutExtension(fileName));
var resultDTO = JsonConvert.DeserializeObject<TestVariable>(result);
return Json(resultDTO);
}
catch (Exception e)
{
var result = "Server is not running";
return Ok(new { ErrorMessage = result });
}
}
<强> TestVariable.cs 强>
public class TestVariable
{
public static int fileName { get; set; }
}
<强> TestClient.cs 强>
public class TestClient
{
private static HttpClient client;
private static string BASE_URL = "http://localhost:8080/";
static TestClient()
{
client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<string> runbatchfile(string fileName)
{
var endpoint = string.Format("runbatchfile/{0}", fileName);
var response = await client.GetAsync(endpoint);
return await response.Content.ReadAsStringAsync();
}
}
更好的解决方案
实际上问题不是扩展名,而是你文件名中的“点”,所以如果你还需要指定文件类型,你可以添加另一个url参数来做到这一点,如下所示: / p>
http://localhost:62198/api/runbatchfile/test/bat
http://localhost:62198/api/runbatchfile/test/exe
http://localhost:62198/api/runbatchfile/test/cmd
第一个参数是文件名,第二个是扩展名,然后在您的控制器操作中,您只需使用以下两个参数生成完整文件名:
[...]
public ResultFormat runbatchFile(String fileName, String ext) {
RunBatchFile rbf = new RunBatchFile();
return rbf.runBatch(fileName, ext);
}
[...]
public ResultFormat runBatch(String fileName, String ext) {
String var = fileName + "." + ext;
[...]
}
在您的ASP.NET客户端中:
var file = Path.GetFileNameWithoutExtension(fileName);
var ext = Path.GetExtension(fileName).Replace(".", String.Empty);
var result = await
testClient.runbatchfile(file, ext);