Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..


In my web application user can upload the documents(abstracts) for this i have created one folder for storing the documents. Presently am storing the path in DB and files in a particular folder(this folder will be created in server). But my problem is, when i restart the server the folder is not visible. files are not opening. what is the problem in my code.. Below is my java code.


Java
public class PostAbstractDao {

	public static PostAbstract insert(PostAbstract bean) {

		try {

			PathReturn path = new PathReturn();
			String realpath = path.getPath();// it will return the path of the myeclipse server(E:\MyeclipseApps\.metadata\.me_tcat\webapps\MedicalConference)

			System.out.println("path is" + realpath);

			int gid = bean.getGid();
			String email = bean.getEmail();
			FormFile file = bean.getFile();

			// Get the servers upload directory real path name

			String filePath = realpath + "abstracts";
			String dbpath = "D:\\abstracts\\" + file.getFileName();
			// create the upload folder if not exists
			File folder = new File(filePath);
			if (!folder.exists()) {
				folder.mkdirs();
			}

			String fileName = file.getFileName();

			if (!("").equals(fileName)) {

				System.out.println("Server path:" + filePath);
				File newFile = new File(filePath, fileName);

				if (!newFile.exists()) {
					FileOutputStream fos = new FileOutputStream(newFile);
					fos.write(file.getFileData());
					fos.flush();
					fos.close();
				}
			}
			Connection con = (Connection) new DB2Connection()
					.getDatabaseConnection();

			String sql = "insert into user_posts(gid,posts,email) values(?,?,?) ";

			PreparedStatement ps = con.prepareStatement(sql);
			ps.setInt(1, gid);

			ps.setString(2, dbpath);
			ps.setString(3, email);

			int ok = ps.executeUpdate();
			if (ok > 0) {

				bean.setValid(true);
			}

		} catch (SQLException e) {

			e.printStackTrace();

		} catch (NullPointerException nl) {
			nl.printStackTrace();
		}

		catch (Exception ex) {

			ex.printStackTrace();

		}

		return bean;

	}

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900