How To Change The File Last Modified Date In Java?
Here’s an example to change the file’s last modified date with the help of File.setLastModified() . This method accept the new modified date in milliseconds (long type), So some data type conversion are required.
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ChangeFileLastModifiedDate {
public static void main(String[] args) {
try {
File file = new File("C:\File.pdf");
// Print the original last modified date
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Original Last Modified Date : "
+ sdf.format(file.lastModified()));
// Set this date
String newFileDate = "11/17/2000";
// Need convert the above date to milliseconds in long value
Date newDate = sdf.parse(newFileDate);
file.setLastModified(newDate.getTime());
// Print the latest last modified date
System.out.println("Lastest Last Modified Date : "
+ sdf.format(file.lastModified()));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Result :
Original Last Modified Date : 12/20/2011
Lastest Last Modified Date : 11/17/2000
How To Get The File’s Last Modified Date & time In Java?
In Java, you can use the File.lastModified() to get the file’s last modified timestamps. This method will returns the time in milliseconds (long value), you may to format it with SimpleDateFormat to make it a human readable format.
File Last Modified Date and Time
import java.io.File;
import java.text.SimpleDateFormat;
public class GetFileLastModifiedDate
{
public static void main(String[] args)
{
File file = new File("c:\Core java.ppt");
System.out.println("Before Format : " + file.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println("After Format : " + sdf.format(file.lastModified()));
}
}
Result :
Before Format : 1359018283231
After Format : 03/24/2013 12:34:33
Struts 2 Examples Files
![]() |
|
![]() |
|
Read More
Struts 2 <s:textfield> example
In Struts 2 , you can use the <s:textfield> to create a HTML input textbox. For example, you can declare the “s:textfield” with a key attribute or label and name attribute.
<s:textfield key="userName" /> //or <s:textfield label="Username" name="userName" />
In Struts 2, the “name” will map to the JavaBean property automatically. In this case, on form submit, the textbox value with “name=’username’” will call the corresponding Action’s setUsername(String xx) to set the value
Struts 2 <s:textfield> example
Quick guide to create a textbox input field in Struts 2.
1. Properties file
Two properties files to store the message.
project.properties
project.username = Username project.submit = Submit
LoginAction.properties
username.required = Username Cannot be blank
2. Action
A simple Action class with a validation to make sure the username is not empty, otherwise return an error message.
LoginAction.java
package com.simplecode.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 6677091252031583948L;
// Need to Initiize a morden driven object
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
return SUCCESS;
}
public void validate() {
if (userName.equals("")) {
addFieldError("userName", getText("username.required"));
}
}
}
3. View page
Result page to use Struts 2 “s:textfield” to create a HTML textbox input field.
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login</title> </head> <body> <h3>Struts 2 < S:textfield > Textbox Examplee</h3> <s:form action="welcome"> <s:textfield name="userName" key="project.username" /> <s:submit key="project.submit" name="submit" /> </s:form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Welcome Page</title> </head> <body> <h3>Struts 2 < S:textfield > Textbox Example</h3> <h4> Welcome <s:property value="userName" /> </h4> </body> </html>
4. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="project" /> <package name="default" extends="struts-default" namespace="/jsp"> <action name="Login"> <result>/jsp/login.jsp</result> </action> <action name="welcome" class="com.simplecode.action.LoginAction"> <result name="success">/jsp/success.jsp</result> <result name="input">/jsp/login.jsp</result> </action> </package> </struts>
5. Demo
http://localhost:8089/Struts2_textfield/
Reference
1. Struts 2 textfield documentation
Struts 2 File Upload Example
In this example you will learn how to do file upload with the help of the built-in FileUploadInterceptor.
In Struts 2, the <s:file> tag is used to create a HTML file upload component to allow users to select file from their local disk and upload it to the server. In this tutorial, you will create a JSP page with file upload component, set the maximum size and allow content type of the upload file, and display the uploaded file details.
How File Upload Works in Struts 2?
The file upload function depends on the “fileUpload Interceptor, to add support for uploading files in the Struts applications. The Struts 2 File Upload Interceptor is based on MultiPartRequestWrapper, which is automatically applied to the request if it contains the file element. Then it adds the following parameters to the request (assuming the uploaded file name is MyFile).
- [MyFile] : File – the actual File
- [MyFile]ContentType : String – the content type of the file
- [File Name]FileName : String – the actual name of the file uploaded (not the HTML name)
In the action class you can get the file, the uploaded file name and content type by just creating getter and setters.
For example
private File uploadDoc;
This will store actual uploaded File
private String uploadDocFileName;
This string will contain the Content Type of uploaded file.
private String uploadDocContentType;
This string will contain the file name of uploaded file.
The fields uploadDocContentType and uploadDocFileName are optional. If setter method of these fields are provided, struts2 will set the data. This is just to get some extra information on the uploaded file. Also follow the naming standard if you are providing the content type and file name string. The name should be FileName and ContentType. For example if the file attribute in action file is private File uploadedFile, the content type will be uploadedFileContentType and file name uploadedFileFileName.So make sure the method name is spelled correctly.
The file upload interceptor also does the validation and adds errors. These error messages are stored in the struts-messsages.properties file. The values of the messages can be overridden by providing the text for the following keys:
- struts.messages.error.uploading - error when uploading of file fails
- struts.messages.error.file.too.large - error occurs when file size is large
- struts.messages.error.content.type.not.allowed - when the content type is not allowed
Parameters
You can use the following parameters to control the file upload functionality.
- maximumSize This parameter is optional. The default value of this is 2MB.
<param name="maximumSize">1024000</param>
You can also set the upload file size using the following code
<constant name="struts.multipart.maxSize" value="20097152" />
- allowedTypes This parameter is also optional. It allows you to specify the allowed content type.
- To upload txt file
<param name="allowedTypes"> text/plain </param>
- To upload PDF files
<param name="allowedTypes"> application/pdf</param>
- To upload any files
<param name="allowedTypes"> application/pdf</param>
1. Folder Structure

2. Action class
FileUploadAction.java
package com.simplecode.action;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File uploadDoc;
private String uploadDocFileName;
private String uploadDocContentType;
public String execute() {
return SUCCESS;
}
public String display() {
return NONE;
}
public File getUploadDoc() {
return uploadDoc;
}
public void setUploadDoc(File uploadDoc) {
this.uploadDoc = uploadDoc;
}
public String getUploadDocFileName() {
return uploadDocFileName;
}
public void setUploadDocFileName(String uploadDocFileName) {
this.uploadDocFileName = uploadDocFileName;
}
public String getUploadDocContentType() {
return uploadDocContentType;
}
public void setUploadDocContentType(String uploadDocContentType) {
this.uploadDocContentType = uploadDocContentType;
}
}
3. Result Page
fileupload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>file upload page</title> </head> <body> <h3>Struts 2 file upload example</h3> <s:form action="fileUpload" method="post" enctype="multipart/form-data"> <s:file name="uploadDoc" label="Choose file to upload" /> <s:submit value="upload" align="center" /> </s:form> </body> </html>
uploadSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<style type="text/css">
.uploaded {
background-color: #DDFFDD;
border: 1px solid #009900;
width: 700px;
}
.welcome li {
list-style: none;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Successful</title>
<s:head />
</head>
<body>
<h3>Struts 2 file upload example</h3>
<div class="uploaded">
File Name :
<s:property value="uploadDocFileName"></s:property>
<br /> Content type:
<s:property value="uploadDocContentType"></s:property>
<br /> User file :
<s:property value="uploadDoc"></s:property>
</div>
</body>
</html>
4. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="" namespace="/jsp" extends="struts-default"> <action name="fileUploadAction" class="com.simplecode.action.FileUploadAction" method="display"> <result name="none">/jsp/fileUpload.jsp</result> </action> <action name="fileUpload" class="com.simplecode.action.FileUploadAction" > <interceptor-ref name="exception" /> <interceptor-ref name="i18n" /> <interceptor-ref name="fileUpload"> <param name="allowedTypes">application/pdf</param> <param name="maximumSize">1024000</param> </interceptor-ref> <interceptor-ref name="params"> <param name="excludeParams">dojo..*,^struts..*</param> </interceptor-ref> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <result name="success">/jsp/uploadSuccess.jsp</result> <result name="input">/jsp/fileUpload.jsp</result> </action> </package> </struts>
5. Demo
http://localhost:8089/Struts2_FileUpload/

Error message prompts if you upload a file which is more than 10kb, or not a text file.

The uploaded file will be treat as a temporary file, with a long random file name, upload__376584a7_12981122379__8000_00000010.tmp. This file will be stored some where in the metadata directory

Note:
In order to save the .tmp file in a desired location ,which you wish , then use the following code in your struts.xml<constant name="struts.multipart.saveDir" value="D:/your folder"/>
Reference
- Struts 2 file documentation
Read More

