又好久没特么写笔记了,这样岂能进步与辉煌,要鞭策自己常写!
先来个简单的:
工程目录结构如下:
SmartUpload先简单用一下文件下载的处理。简单的运用,我就直接用Servlet进行操作了。
1、先建立一个指向页面的ToDownloadFileServlet,并且读取指定路径下的所有文件列表,展示在页面:
(在这之前配置servlet的时候,没注意@WebServlet("/toDownloadFile")这个注解,导致tomcat启动失败等很恶心的问题,切记仔细)
@WebServlet("/toDownloadFile")
public class ToDownloadFileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.sendRedirect("WEB-INF/jsp/help/downloadFile.jsp");
String path = this.getServletContext().getRealPath("/FileDownload") ;
File f = new File(path);
if(!f.exists()) {
System.out.println(path + "路径不存在");
}
File[] file = f.listFiles();
List<String> fileNameList = new ArrayList<String>();
for(int i = 0; i < file.length; i++) {
if(file[i].isDirectory()) {
i++;
continue;
}
fileNameList.add(file[i].getName());
}
request.setAttribute("fileNameList", fileNameList);
request.getRequestDispatcher("WEB-INF/jsp/help/downloadFile.jsp").forward(request, response);
}
}
2、jsp页面的相关代码贴上来(这部分简单)
<div class="rightinfo">
<table id="table" class="tablelist">
<thead>
<tr>
<th width="50px">序号</th>
<th width="200px">文件名称</th>
<th width="100px">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${fileNameList }" var="fileName" varStatus="status">
<tr>
<td>${status.index+1 }</td>
<td>${fileName }</td>
<td><a href="downloadFile?fileName=${fileName }">下载</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
3、用于文件下载的DownloadFileServlet
@WebServlet("/downloadFile")
public class DownloadFileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = request.getParameter("fileName");
SmartUpload su = new SmartUpload();
su.initialize(getServletConfig(), request, response);
su.setContentDisposition(null);
try {
su.downloadFile("/FileDownload/" + fileName);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
基本就是这样子了,很简单,后面会弄一下springMVC的上传和下载!