我正在尝试使用HttpURLConnection向网址发送帖子请求(在java中使用cUrl).请求的内容是xml,在结束时,应用程序处理xml并将记录存储到数据库,然后以xml字符串的形式发回响应.该应用程序在本地托管在apache-tomcat上.
当我从终端执行此代码时,会按预期将一行添加到数据库中.但是从连接获取InputStream时会抛出异常,如下所示
java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)
at org.kodeplay.helloworld.HttpCurl.main(HttpCurl.java:30)
Run Code Online (Sandbox Code Playgroud)
这是代码
public class HttpCurl {
public static void main(String [] args) {
HttpURLConnection con;
try {
con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
File xmlFile = new File("test.xml");
String xml = ReadWriteTextFile.getContents(xmlFile);
con.getOutputStream().write(xml.getBytes("UTF-8"));
InputStream response = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
for (String line ; (line = reader.readLine()) != null;) {
System.out.println(line);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException …
Run Code Online (Sandbox Code Playgroud) 出于某种原因,我的代码无法打开一个简单的文件:
这是代码:
file1 = open('recentlyUpdated.yaml')
Run Code Online (Sandbox Code Playgroud)
错误是:
IOError: [Errno 2] No such file or directory: 'recentlyUpdated.yaml'
Run Code Online (Sandbox Code Playgroud)
open()
的完整路径,似乎没有任何工作.我在某些设备上访问存储时遇到了一个非常奇怪的问题.该应用程序适用于我的测试设备(Nexus 4和7,三星GS5).我的所有设备都运行Android 4.4.2.但是我收到了很多用户的电子邮件,说应用程序无法写入存储(内部存储和SD卡).从用户反馈收到的日志文件中,我可以看到问题是以下代码:
try {
if (fStream == null) {
fStream = new FileOutputStream(filename, true);
}
fStream.write(data, 0, bytes);
return;
} catch (IOException ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
它在fStream = new FileOutputStream(filename,true)行抛出异常; 在创建FileOutputStream时.
堆栈日志是:
W/System.err( 8147): Caused by: java.io.FileNotFoundException: /storage/emulated/0/my_folder/test_file_name.png: open failed: EACCES (Permission denied)
w/System.err( 8147): at libcore.io.IoBridge.open(IoBridge.java:409)
W/System.err( 8147): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
W/System.err( 8147): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
W/System.err( 8147): at myapp.save(SourceFile:515)
W/System.err( 8147): ... 8 more
W/System.err( 8147): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
W/System.err( 8147): at libcore.io.Posix.open(Native Method) …
Run Code Online (Sandbox Code Playgroud) 我们有一个Web应用程序,它使用Java applet来操作本地磁盘上的文件.我们开发它已经有一段时间了,我们已经知道所有类型的applet可能与现代操作系统和浏览器以及最新的Java版本和新的安全限制有关.
昨天,Apple推出了新的Mac OS 10.9 Mavericks,推出了新的Safari浏览器(7.0).我在Safari 7/Mac OS X 10.9下测试了我们的Web应用程序,发现Safari 7(可能是?)阻止从Java applet访问本地文件.
尽管applet(使用有效的Thawte证书签名,并且满足Java 7u45特定的所有安全性要求)以无限制模式运行,并且可以完全访问本地文件系统(Java安全提示符说),但在尝试访问本地文件时,它会捕获fileNotFoundException :
java.io.FileNotFoundException: /Users/yury/Pictures/Paris 2012/L1050258.jpg (Operation not permitted)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at com.trackntag.a.v.a(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
此外,当从applet打开Java文件打开对话框时,它不显示任何文件,文件夹选择器中的文件夹没有任何图标(通常他们应该有它们).当您遍历到根文件夹时,您可以看到顶级文件夹(bin,cores,dev,home等),但您无法浏览它们.
applet可以在同一台机器上使用最新的Firefox 24 for Mac访问本地文件(Java 7u45,Mac OS X 10.9).与Safari 6和Mac OS X 10.8.5相同,更不用说各种配置的Linux和Windows机器:访问本地文件没有问题.
综上所述,我必须得出结论,我们遇到了Safari 7特有的问题.
你对这个问题有什么看法吗?任何想法都非常感谢.
干杯,尤里
编辑(答案):在Safari 7中有一个新的安全设置:安全/不安全模式(我认为它只适用于Java插件).您可以允许Java插件在不安全模式下为单个网站或所有站点工作.在不安全模式下,applet将具有对本地文件系统的无限制访问权限.
看来这个安全设置在Java自己的安全设置(受限制/不受限制的访问)之上工作.
此设置可通过Safari首选项/安全选项卡/ Internet插件获得:管理网站设置(然后选择Java插件).
所以,一旦我已经回答了我自己的原始问题,我宁愿将其改为:是否可以设置不安全模式或覆盖特定网站/ URL的安全模式而无需要求用户设置此安全首选项?也许Apple开发者证书之类的东西会有帮助吗
再次感谢!
我有一个名为" word.txt
" 的文件.
它与我的java
文件位于同一目录中.
但是,当我尝试在以下代码中访问它时,发现此文件未找到错误:
Exception in thread "main" java.io.FileNotFoundException: word.txt
(The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Hangman1.main(Hangman1.java:6)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("word.txt"));
String in = "";
in = input.nextLine();
}
}
Run Code Online (Sandbox Code Playgroud) 有没有其他方法可以检查Windows应用商店应用中是否存在文件?
try
{
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.xml");
//no exception means file exists
}
catch (FileNotFoundException ex)
{
//find out through exception
}
Run Code Online (Sandbox Code Playgroud) c# filenotfoundexception microsoft-metro windows-8 windows-runtime
我试图读取文件夹中的文件,但是当我运行该程序时,它会抛出此异常.我也尝试过其他一些文件夹.它引发了同样的异常.
Exception in thread "main" java.io.FileNotFoundException: C:\backup (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
Run Code Online (Sandbox Code Playgroud) 我有一个适用于Android 2.x和3.x的Android应用程序,但在Android 4.x上运行时会失败.
问题出在这部分代码中:
URL url = new URL("http://blahblah.blah/somedata.xml");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)
当应用程序在Android 4.x上运行时,getInputStream()
调用会产生一个FileNotFoundException
.当在早期版本的Android上运行相同的二进制文件时,它会成功.URL也适用于Web浏览器和curl
.
显然HttpURLConnection
,ICS中有些变化.有没有人知道发生了什么变化,和/或修复可能是什么?
如果我连接到似乎是Apache服务器的代码,下面的代码工作得很好,但是当我尝试连接到我的.Net服务器时,它会抛出错误.我猜这是一个标题要求,但无论我尝试什么,我似乎无法得到成功的回应.
public String Download(String Url)
{
String filepath=null;
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = new URL(Url);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the …
Run Code Online (Sandbox Code Playgroud) 我有一个脚本,提示用户输入文件名(要打开的文件),如果当前目录中不存在该文件,则再次提示用户.这是简短的版本:
file = input("Type filename: ")
...
try:
fileContent = open(filename, "r")
...
except FileNotFoundError:
...
Run Code Online (Sandbox Code Playgroud)
当我在Python 3.3x中使用我的MacOS X测试我的脚本时,当我故意键入错误的文件名时它完全正常(它在"expect"下执行套件).
但是,当我想在Python 3.2x中的Windows计算机上运行我的代码时,我收到一条错误,指出未定义"FileNotFoundError".因此,Windows上的Python 3.2认为"FileNotFoundError"是一个变量,程序退出并出现错误.
我发现如果输入文件名无效,Windows上的Python 3.2会抛出"IOError".我在我的Linux机器上用Python 2.7测试过,它也是一个IOError.
我现在的问题是,代码用
except "FileNotFoundError":
Run Code Online (Sandbox Code Playgroud)
将无法在Windows的Python 3.2上运行,但如果我将其更改为
except "IOError":
Run Code Online (Sandbox Code Playgroud)
它不再适用于我的Mac.
我怎么能解决它?我能想到的唯一方法是使用just
except
,我通常不想要.