Home > Web Front-end > JS Tutorial > Create folders and files in JSP

Create folders and files in JSP

巴扎黑
Release: 2016-12-20 15:11:37
Original
2611 people have browsed it

Determine whether a file exists in JSP and create folders and files.

Memo.

In WinXP + Tomcat5.1, the code is as follows:

<%

//Get the web root path //Absolute path
//getServletContext().getRealPath("/") Get the root path of the web application
// D:webexcel, "D:web" is the root path of the web application, "excel" is the folder in the root directory
String Save_Location=getServletContext().getRealPath("/")+"excel\";

try{
if (!(new java.io.File(Save_Location).isDirectory())) //If the folder does not exist
{
new java.io.File(Save_Location).mkdir(); //Does not exist excel folder, create this folder
new java.io.File(Save_Location)+"gmcc\").mkdir(); //Create a folder named gmcc under the excel folder
}
else //Exists excel folder, create this folder directly
{
new java.io.File(Save_Location)+"gmcc\").mkdir(); //Create a folder named gmcc under the excel folder
}
} catch(Exception e){
e.printStackTrace(); //Failed to create folder

//Use URLEncoder encoding in the link to pass Chinese parameters.
//The receiving page can use getParameter() to obtain this parameter, the page's charset=GB2312.
String ErrName=java.net.URLEncoder.encode("The folder does not exist. Error creating the folder!");
response.sendRedirect("errorpage.jsp?error="+ErrName); //Jump to the error page
return;
}

//Create a new myfile.txt file in the gmcc folder
java.io.File myFile = new java.io.File(Save_Location+"gmcc\myfile.txt");
java.io.FileOutputStream fout = null;
try {
fout = new java.io.FileOutputStream(myFile);
byte b[]= "Hello!".getBytes();
fout.write(b);
fout.flush() ; //Write file
fout.close(); //Close
}
catch (java.io.FileNotFoundException e) {
e.printStackTrace();
}
catch (java.io.IOException ex) {
ex.printStackTrace();
}

%>

J.R.Q.


Related labels:
jsp
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template