After populating the list of files in the jsp(view) part, you can have an anchor link either on their name or any other properties.
I have used in their names.
Simply use this link in their names in your jsp page:
<td><a href="../download.htm?file=<c:out value='${files.fileLocation}'/>&fileName=<c:out value='${files.fileName}'/>"/><c:out value="${files.fileName}"/></a></td>
Now make your entry for the download.htm in the dispatcher servlet:
<bean id="downloadFileController" class="com.d2.vhippm.impl.web.controller.DownloadFilesController">
<property name="filePath" value="VHIPPMFiles/Clients"/>
</bean>
Finally here goes the controller:
//you can modify this as you desire
//this controller allows the user to download the file whose link was clcked by the user
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
*
* @author Ramesh Raj Baral
* @since:Jan 15, 2010
* File:DownloadFilesController.java
* @Version:1.0
*/
public class DownloadFilesController implements Controller {
private String filePath;
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ServletContext servletContext = request.getSession()
.getServletContext();
String file = request.getParameter("file");
String fileName = request.getParameter("fileName");
String tempDestPath=new File(new File(".").getCanonicalPath()).toString()+System.getProperty("file.separator")+this.filePath+System.getProperty("file.separator")+fileName;
File uFile = new File(tempDestPath);
System.out.println("filepath:"+tempDestPath);
System.out.println("File exist:"+uFile.exists()+"..readable:"+uFile.isFile());
try{
uFile=new File(tempDestPath);
}
catch(Exception ex){
System.out.println("exception getting file:"+ex.getMessage());
}
int fSize = (int) uFile.length();
if (fSize > 0) {
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(uFile));
String mimetype = servletContext.getMimeType(file);
response.setBufferSize(fSize);
response.setContentType(mimetype);
response.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
response.setContentLength(fSize);
//added later
/**response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
*/
//finish added later
FileCopyUtils.copy(in, response.getOutputStream());
in.close();
response.getOutputStream().flush();
response.getOutputStream().close();
} else {
response.setContentType("text/html");
PrintWriter printwriter = response.getWriter();
printwriter.println("<html>");
printwriter.println("<br><br><br><h2>Could not get file name:<br>" + file + "</h2>");
printwriter.println("<br><br><br><center><h3><a href='javascript: history.go(-1)'>Back</a></h3></center>");
printwriter.println("<br><br><br>© webAccess");
printwriter.println("</html>");
printwriter.flush();
printwriter.close();
}
return null;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
Safe exception handling is customizable as you wish.
Thanks a lot for the posts at: http://forum.springsource.org/showthread.php?t=14128
Thank you.
I used this code to restrict certain downloads in my spring app. Everytime I download a file through it, though, the jvm's memory grows. If I do it enough times, there's a heap space error. Are there any resources that need to be released? I can't think of any.
ReplyDeleteare you talking about perm gen space problem?
ReplyDeleteyes.
ReplyDeleteadd this (-XX:MaxPermSize=512m) to the jboss arguments if you are using jboss and this prevents the heap space issue.
ReplyDeleteTo go to this first double click on the server and then click on the Open Launch Configuration. Now select the Arguments tab and paste the above specified value to the VM Arguments.
By doing this you are increasing the heap space that will be taken by JVM.
This code is not responsible for generating the heap space issue. Heap issue may arise at anytime when JVM doesnot find the required memory/space to be used for its operation. As file operation is costly and require more space than other normal operations, this can occur due to the File IO operation.
ReplyDelete