我正在使用 ASP.NET Web 服务调用 Java Web 服务。 Java Web 服务执行用户在 URL 中指定的批处理文件(例如 http://localhost:8080/runbatchfile/test.bat
)。
ASP.NET Web 服务充当 API,应该调用 Java Web 服务并返回当我键入 URL http://localhost:62198/api/时 Java Web 服务返回的数据运行 ASP.NET Web 服务时运行batchfile/test.bat
。
但是,我无法使用 ASP.NET Web 服务检索和显示数据,我认为这是由 .bat 扩展名引起的。当我调用没有参数或参数仅涉及数字的 Java Web 服务时,该 ASP.NET Web 服务可以工作,但是当涉及扩展时我无法获取结果。
如果执行批处理文件,我应该得到的结果是 {"Result": true}
,如果不执行批处理文件,我应该得到的结果是 {"Result": false}
。但是,我得到一个空的 {}
。但当我运行 Java Web 服务时,它会正确显示结果。只有 ASP.NET Web Service 无法从 Java Web Service 读取数据并显示。
我应该添加哪些代码才能包含 .bat 扩展名?请有人帮助我,提前非常感谢。
这是我到目前为止所做的:
JAVA代码
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();
}
}
最佳答案
在 URL 中添加“.bat”扩展名可能不是最好的方法。 Web 服务器无法识别此扩展,您需要进行一些调整才能允许它,而且它还可能导致一些您无法控制的安全问题,因为 .bat 文件被视为可执行文件。
事实上,如果您知道该文件始终是“.bat”文件,您是否可以完全省略 URL 中的扩展名并使用如下所示的内容:
http://localhost:62198/api/runbatchfile/test
然后您只需将其添加到代码中即可:
JAVA代码
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 参数来执行此操作,如下所示:
http://localhost:62198/api/runbatchfile/test/bat
http://localhost:62198/api/runbatchfile/test/exe
http://localhost:62198/api/runbatchfile/test/cmd
第一个参数是文件名,第二个参数是扩展名,然后在 Controller 操作中,您只需使用这两个参数生成完整的文件名:
[...]
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);
关于java - 调用 Java Web Service 时包含 .bat 扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493778/