I am writing a logic where the user uploads a text file when user visits the webpage for the first time(eg. in his profile page). The next time when the user visits the webpage he should be able to find the uploaded file as a link.
So far i have been able to upload the file in a user specific path and dwelling on how to show it in the webpage.
I am following mkyong but the problem i find here is the action result is in stream whereas in my case the valuestack has other action details.
Code Fragment :
1.Action Class
public String uploadResume(){
try{
if(uploadFile==null){
throw new NullPointerException();
}
File file = new File("/Users/shibasish/Documents/","/"
+ GenerateTimestampID.generateTimestamp()+"/shibasish");
if (!file.exists()) {
if (file.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
String filePath = file.getPath();
File fileToCreate = new File(filePath, uploadFileFileName);
FileUtils.copyFile(uploadFile, fileToCreate);
}catch(NullPointerException e){
addActionError("Please upload a resume");
}
catch(Exception e){
e.printStackTrace();
}
return "success";
}
2.struts.xml
<action name="uploadResume" class="com.msventure.web.actions.CompleteProfileAction"
method="uploadResume">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">
text/plain,application/pdf,application/octet-stream
</param>
</interceptor-ref>
<result name="success">/profile.jsp</result>
<result name="fail">/login.jsp</result>
<result name="index">/index.jsp</result>
<result name="login">/talent.jsp</result>
</action>
JSP
<form id="login" name="login" method="post" action="signup">
<p>
<input type="submit" value="Update" />
</p>
</form>
<s:if test="uploadFile neq null">
<!--<h4>Download file - <s:a href="uploadFile">fileABC.txt</s:a></h4>-->
<a href="<s:property value="uploadFile" />" />
</s:if>
<s:form id="login" name="login" method="post" action="uploadResume"
enctype="multipart/form-data">
<s:file name="uploadFile" label="Select a File to upload" size="40"/>
<s:submit value="submit" name="submit"/>
<!--<input type="button" value="Search" id="resumeupload" />-->
</s:form>
Please let me know in case i am not clear.