How to upload files on multiple platforms using Java?

I have a Java Swing database application that should be running on Windows and Linux. Information about connecting to my database is stored in an XML file, and I download it.

This application may correctly load these properties on Linux, but it does not work on Windows.

How to properly upload files on multiple platforms using Java?


This is the code:

PropertyHandler propertyWriter = new PropertyHandler();

List keys = new ArrayList();
keys.add("ip");
keys.add("database");
Map localProps = propertyWriter.read(keys, "conf" + File.separatorChar + "properties.xml", true);//if false load from the local properties

//get properties from the xml in the internal package
List seKeys = new ArrayList();
seKeys.add("driver");
seKeys.add("username");
seKeys.add("password");

Map seProps = propertyWriter.read(seKeys, "conf" + File.separatorChar + "properties.xml", true);

String dsn = "jdbc:mysql://" + (String) localProps.get("ip") + ":3306/" + (String) localProps.get("database");
jDBCConnectionPool = new JDBCConnectionPool((String) seProps.get("driver"), dsn, (String) seProps.get("username"), (String) seProps.get("password"));

File Reading Method:

public Map read(List properties, String path, boolean isConfFromClassPath)
{
    Properties prop = new Properties();
    Map props = new HashMap();
    try {

        if (isConfFromClassPath) {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
            prop.loadFromXML(in);

            for (Iterator i = properties.iterator(); i.hasNext();) {
                String key = (String) i.next();
                props.put(key, prop.getProperty(key));
            }
            in.close();

        } else {
            FileInputStream in = new FileInputStream(path);
            prop.loadFromXML(in);

            for (Iterator i = properties.iterator(); i.hasNext();) {
                String key = (String) i.next();
                props.put(key, prop.getProperty(key));
            }
            in.close();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return props;
}
+3
source share
5 answers

If the file is in a jar file and is accessible to the classpath, you should always use /.

JavaDocs for ClassLoader.getResourcesays that "the name of the resource is the path to the" / "- separated path that defines the resource."

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

+5

, , :

File confDir = new File("conf");
File propFile = new File(confDir, "properties.xml");

, , , /

+1

, , :

getClass().getClassLoader().getResourceAsStream(
    "/META-INF/SqlQueryFile.sql")));
0

.

, Matcher.quoteReplacement(File.separator) .

.

String fileLocation = "/src/service/files";

fileLocation  = fileLocation.replaceAll("/",Matcher.quoteReplacement(File.separator));
0

, conf/properties.xml Linux conf\properties.xml Windows, File.pathSeparator File.separator

-1

All Articles