我想为连接的网址创建一个文件夹结构。我能够从连接的URL位置读取文件,但请注意能够创建文件夹。这是我从URL读取文件的代码。
public class ReadFromURL {
public static void main(String[] args) {
// TODO Auto-generated method stub
String urlToConnect = "http://11.111.111.2/UploadedFiles/test/";
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
URLConnection connection = null;
URL url = null;
try {
url= new URL("http://11.111.111.2/UploadedFiles/test");
connection = url.openConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.setDoOutput(true); // This sets request method to POST.
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
int responseCode = 0;
try {
responseCode = ((HttpURLConnection) connection).getResponseCode();
URL url1 = new URL("http://11.111.111.2/UploadedFiles/test/test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(url1.openStream()));
String FILENAME = "D:\\test\\filename.txt";
// write the output to stdout
String line;
while ((line = reader.readLine()) != null)
{
write_to_file(FILENAME,line);
System.out.println(line);
}
// close our reader
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(responseCode);
}
public static void write_to_file(String filename,String line)
{
BufferedWriter bw = null;
FileWriter fw = null;
try {
//String content = "This is the content to write into file\n";
fw = new FileWriter(filename);
bw = new BufferedWriter(fw);
bw.write(line);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}