There is file PULS.TXT on URl mobile.puls-radio.ru
How can I read this file from java code ?
I use this code, but does not work
URL url = null;
try {
url = new URL("http://mobile.puls-radio.ru/PULS.txt");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
InputStream is = url.openStream();
Scanner s = new Scanner(is).useDelimiter("\\A");
Log.i("TEST", s.hasNext() ? s.next() : "");
} catch (IOException e) {
e.printStackTrace();
}
Related
I am using apache common library for connecting to FTP with Android app.
Now I want to upload a file from internal storage to FTP server and I get this reply from getReplyString() method.
And I get this msg
553 Can't open that file: Permission denied
//Write file to the internal storage
String path = "/sdcard/";
File file = new File(path, fileName);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(file);
stream.write(jsonObject.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Read the file from resources folder.
try {
File file1 = new File(path, fileName);
Log.d("path",file1.getPath());
BufferedInputStream in = new BufferedInputStream (new FileInputStream (file1.getPath()));
client.connect(FTPHost);
client.login(FTPUserName, FTPPassword);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
// Store file to server
Log.d("reply",client.getReplyString());
boolean res = client.storeFile("/"+fileName, in);
Log.d("reply",client.getReplyString());
Log.d("result",res+"");
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
i have a problem with download a textfile. I have no file contents after the dowload, the downloaded file is empty. Android and Core Api
#Override
protected Boolean doInBackground(Void... params) {
FileOutputStream outputStream = null;
try {
File fileDown = new File(LOCAL_PATH_DOWNLOAD);
outputStream = new FileOutputStream(fileDown);//
DropboxAPI.DropboxFileInfo info = mApi.getFile(DROPBOX_FILE_DIR_DOWNLOAD, null, outputStream, null);
return false;
} catch (Exception e) {
System.out.println("Something went wrong: " + e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
}
return false;
}
What do i wrong? Thanks for help
i use above code to do this
File file= new File("/sdcard/New_csv_file.csv");
OutputStream out= null;
boolean result=false;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
DropboxFileInfo info = mApi.getFile("/photos/New_csv_file.csv", null, out, null);
Log.i("DbExampleLog", "The file's rev is: " + info.getMetadata().rev);
Intent JumpToParseCSV=new Intent(context,ParseCSV.class);
JumpToParseCSV.putExtra("FileName", file.getAbsolutePath());
Log.i("path", "FileName"+ file.getAbsolutePath());
((Activity) context).finish();
context.startActivity(JumpToParseCSV);
result=true;
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while downloading.");
file.delete();
result=false;
}
return result;
I try a lot of thinks to find the fail but i don't know how I can do it. my code is:
//DominioLlamadaRedSys.java
Properties d = new Properties();
InputStream entrada = null;
try {
entrada = new FileInputStream("prop/datosApp.properties");
d.load(entrada);
System.out.println(d.getProperty("TXD.endPointUrl"));
} catch (IOException ex) {
System.out.println("ERROR: "+ ex.getMessage());
} finally {
if (entrada != null) {
try {
entrada.close();
} catch (IOException e) {
}
}
}
I call the file inside a class in "com.rsi.secpay.dominio" and this always catch the same exception (don't find the file), I had try to quit "prop/" (just "datosApp.properties" ) with properties files like this:
If your prop package is in your classpath, you can get the stream using the classloader:
InputStream is = DominioLlamadaRedSys.class.getResourceAsStream("/prop/datosApp.properties");
I am trying to read the first line of a URL.
Then i want to use that as a string later in the code.
Anyone can help me?
I already tried it with
public static String main(String[] args) {
try {
URL url = new URL("myurlhere");
// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
return line;
}
in.close();
}
catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
return null;
}
I just can't get a string out of it.
You can consider using jsoup for your purpose:
try {
Document doc = Jsoup.connect("http://popofibo.com/pop/swaying-views-of-our-past/").get();
Elements paragraphs = doc.select("p");
for(Element p : paragraphs) {
System.out.println(p.text());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output:
It is indeed difficult to argue over the mainstream ideas of evolution of human civilizations...
If you want to read from a file on the internet using a URL you should use URLConnection
here is a simple example:
String string = "";
try {
URLConnection connection = new URL(
"http://myurl.org/mypath/myfile")
.openConnection();
Scanner scanner = new Scanner(connection.getInputStream());
while (scanner.hasNext()) {
string += scanner.next() + " ";
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
// Do something with the string.
I'm trying to detect a file content type passed to a web service into the SOAP envelop.
This file can be indicated in two ways :
from its url,
from its contain (base64 compressed data).
At this point, I'm able to translate this file into a stream buffer.
But, all my tries to get its content type failed.
The content type is detected if the file extension is indicated otherwise the content is always detected as "plain/text".
Bellow is my class code :
class MetadataAnalyser {
private InputStream _is;
private File _file;
private void initializeAttributes() {
_is = null;
_file= null;
}
private void createTemporaryFile(byte[] pData) {
FileOutputStream fos = null;
try {
_file = File.createTempFile(
UUID.randomUUID().toString().replace("-", ""),
null,
new File("C:\\Users\\Florent\\Documents\\NetBeansProjects\\ServiceEdition\\tmp"));
} catch (IOException e) {
e.printStackTrace();
}
try {
fos = new FileOutputStream(_file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(pData);
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
_file.deleteOnExit();
}
public MetadataAnalyser(byte[] pData) {
initializeAttributes();
_is = new ByteArrayInputStream(pData);
createTemporaryFile(pData);
}
public MetadataAnalyser(InputStream pIs) {
initializeAttributes();
_is = pIs;
_file = null;
}
public MetadataAnalyser(File pFile) {
initializeAttributes();
try {
_file = pFile;
_is = new FileInputStream(_file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public MetadataAnalyser(String pFile) {
initializeAttributes();
try {
_file = new File(pFile);
if (_file.exists()) {
_is = new FileInputStream(_file);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getContentType() {
AutoDetectParser parser = null;
Metadata metadata = null;
InputStream is = null;
String mimeType = null;
parser = new AutoDetectParser();
parser.setParsers(new HashMap<MediaType, Parser>());
metadata = new Metadata();
if(_file != null) {
metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, _file.getName());
}
try {
is = new FileInputStream(_file);
parser.parse(is, new DefaultHandler(), metadata, new ParseContext());
mimeType = metadata.get(HttpHeaders.CONTENT_TYPE);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
} finally {
return mimeType;
}
}
}
So, how to detect the MIME type even if the file extension is unknown ?
I don't think you can detect the mime type without extension , you would need to know which system is writing the file and what kind of file is expected to be there and based on that you need to set the MIME type(I guess you are using it in your response).
You need to make sure the content is decoded before being sent to Tika and no, the extension is absolutely not needed, the detection happens via a well understood mime magic process described here: https://tika.apache.org/1.1/detection.html