Lỗi new File tạo folder thay vì tạo file mới trong Java

Chào các bạn, hiên tại mình có đoạn code tạo file .mht như sau:

@SuppressWarnings({ "unchecked", "rawtypes" })
	public Result<String> parseMessage(String certificateUrl,
			HttpServletRequest request, BigDecimal classId, BigDecimal agentId,
			String language) {
		FileInputStream fis = null;

		txtBody = new StringBuffer();
		htmlBody = new StringBuffer();
		attachments = new ArrayList();
		urlImage = new ArrayList<String>();
		Result<String> result = new Result<String>();

		try {

			String destinationPath = request.getSession().getServletContext()
					.getRealPath(ExcelConstant.EXCEL_TEMP_FOLDER);

			// Get stream from file
			File file = new File(LMSInit.getConfig().getResourceLocation()
					+ Constant.SEPARATOR + certificateUrl);

			fis = new FileInputStream(file);

			
			Message mimeMsg = new Message(fis);
			
			BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(destinationPath + fileName + ".html"),
					"UTF-8"));
			result.setData(ExcelConstant.EXCEL_TEMP_FOLDER + fileName + ".html");

			StringBuffer htmlBodyTemp = new StringBuffer();
			htmlBodyTemp.append(replaceHmlString(htmlBody.toString(), classId,
					agentId, language));

			String strHtmlBody = htmlBodyTemp.toString();
			String characterReplace = "Content-Location:";
			for (String url : urlImage) {
				url = url.substring(url.indexOf(characterReplace)
						+ characterReplace.length(), url.length() - 1);
				url = url.trim();
				String imageName = FilenameUtils.getName(url);

				strHtmlBody = strHtmlBody.replace(url, fileName + "_files"
						+ File.separator + imageName);
			}

			// write contents of StringBuffer to a file
			bwr.write(strHtmlBody);

			// flush the stream
			bwr.flush();

			// close the stream
			bwr.close();

		} catch (IOException ex) {
			result.setStatus(Constant.INTERNAL_SERVER_ERROR);
			result.setErrorCode(ErrorCode.CAN_NOT_VIEW_CERTIFICATE);
			ex.fillInStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException ex) {
					result.setStatus(Constant.INTERNAL_SERVER_ERROR);
					result.setErrorCode(ErrorCode.CAN_NOT_VIEW_CERTIFICATE);
					ex.printStackTrace();
				}
			}
		}
		return result;
	}

Vấn đề mình gặp phải là lúc tạo new File ở trên thì sẽ tạo được file:

C:\workplace_ws9\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\public\course\313260\certificate(UnitLink)_Final2.mht

Nhưng thay vì tạo ra file certificate(UnitLink)_Final2.mht thì nó lại tạo ra folder có tên certificate(UnitLink)_Final2.mht nên FileInputStream không thể đọc được folder này.
Ai biết cho mình hỏi lỗi này do đâu và làm sao thể tạo ra file mht thay vì folder vậy?

Phần tạo tập tin mới trên ổ đĩa là chỗ nào vậy bạn? Trên đoạn mã trên của bạn, chỉ có chỗ dùng FileOutputStream là nó sẽ tự tạo tập tin mới và chắc chắn đó là tập tin chứ không phải thư mục.

Lưu ý: new File(path) khác với File.createNewFile().

2 Likes

Phần tạo tập tin mới trên ổ đĩa là ở chỗ:

File file = new File(LMSInit.getConfig().getResourceLocation()
					+ Constant.SEPARATOR + certificateUrl);

nó sẽ tạo ra folder ở

C:\workplace_ws9\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\public\course\313260\certificate(UnitLink)_Final2.mht


lúc đọc FileInputStream chỗ đoạn code

fis = new FileInputStream(file);

sẽ phát sinh lỗi:

java.io.FileNotFoundException: C:\workplace_ws9\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\public\course\313260\certificate(UnitLink)_Final2.mht (Access is denied.)

Đọc lưu ý của mình chưa?
Chắc là khi bạn gọi new File() thì nó tạo ngay luôn không?
Tập tin/thư mục chỉ tạo trên ổ đĩa khi:

  • Gọi File.createNewFile() hoặc File.mkdir().
  • Bạn tự tạo bằng công cụ khác ngoài viết mã.
3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?