如何使用java在URL中创建文件夹结构?
我想创建一个文件夹结构以连接URL位置。我可以从连接的URL位置读取文件,但请注意,我可以创建文件夹。这是我从URL读取文件的代码如何使用java在URL中创建文件夹结构?,java,Java,我想创建一个文件夹结构以连接URL位置。我可以从连接的URL位置读取文件,但请注意,我可以创建文件夹。这是我从URL读取文件的代码 public class ReadFromURL { public static void main(String[] args) { // TODO Auto-generated method stub String urlToConnect = "http://11.111.111.2/UploadedFiles/te
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();
}
}
}
}
您能告诉我们您收到的错误消息吗?对不起,我没有尝试在该位置创建文件夹结构的任何示例。我需要一些示例。在URL()目录可用之前,我想创建一个Test2目录,如