Получение полного пути файла в JAR в EAR?

В Android вы не можете запускать сетевые действия в главном потоке - вам нужно запускать их в другом потоке, например:

Thread mThread = new Thread(new Runnable() {

@Override
public void run() {
    try  {
        //Put your code that you want to run in here(check your connection for example)
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
});

mThread.start
7
задан Andrew Swan 27 April 2009 в 00:32
поделиться

3 ответа

My current solution is to copy the file to the server's temporary directory, then use the absolute path of the copy:

File tempDir = new File(System.getProperty("java.io.tmpdir"));
File temporaryFile = new File(tempDir, "templateCopy.dot");
InputStream templateStream = getClass().getResourceAsStream("myTemplate.dot");
IOUtils.copy(templateStream, new FileOutputStream(temporaryFile));
String absolutePath = temporaryFile.getAbsolutePath();

I'd prefer a solution that doesn't involve copying the file.

6
ответ дан 7 December 2019 в 07:50
поделиться

Unless the code or application you are passing the URI String to accepts a format that specifies a location within a jar/zip file, your solution of copying the file to a temporary location is probably the best one.

If these files are referenced often, you may want to cache the locations of the extract files and just verify their existance each time they are needed.

1
ответ дан 7 December 2019 в 07:50
поделиться

You should copy the contents to a temporary file (potentially with a cache), because trying to get to internal files of the application container is a dependency you want to avoid. There may not even be an extracted file at all (it can load from the JAR directly).

0
ответ дан 7 December 2019 в 07:50
поделиться
Другие вопросы по тегам:

Похожие вопросы: