单个文件上传与下载实现
??? ??? ?????? <span id="accessoryPath_err" style="color: red;"></span>
??? ??? ?????? </td>
??? ??? ???? </tr>
?
?
/**
??? ?* 文件下载
????? path:文件路径
??? ?*/
?
??? public? void downloadFile( String path, HttpServletResponse response) {
??????? String filePath = uploadFilePath + path;
??????? String fileName = "";
??? ??? ?? try {
??? ??? ?? if(filePath.lastIndexOf("/") > 0) {
??? ??? ?? fileName = new String(filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
??? ??? ?? }else if(filePath.lastIndexOf("\") > 0) {
??? ??? ?? fileName = new String(filePath.substring(filePath.lastIndexOf("\")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
??? ??? ?? }
??? ??? ?? }catch(Exception e) {}
??? ??? ?? FileInputStream fs = null;
??? ??? ?? try {
??? ??? ?? fs = new FileInputStream(new File(filePath));
??? ??? ?? }catch(FileNotFoundException e) {
??? ??? ?? e.printStackTrace();
??? ??? ?? return;
??? ??? ?? }
??? ??? ?? response.reset();
??? ??? ?? response.setContentType("APPLICATION/OCTET-STREAM");
??? ??? ?? response.setHeader("Content-Disposition", "attachment; filename="" + fileName + """);
??? ??? ?? int b = 0;
??? ??? ?? try {
??? ??? ?? PrintWriter out = response.getWriter();
??? ??? ?? while((b=fs.read())!=-1) {
??? ??? ?? out.write(b);
??? ??? ?? }
??? ??? ?? fs.close();
??? ??? ?? out.close();
??? ??? ?? }catch(Exception e) {
??? ??? ?? e.printStackTrace();
??? ??? ?? }
??? ??? ? }
?
?
??? action调用:
?
public void downloadAcc(){
??? ??? ? String path = StrutsUtil.getParameter("path");
??? ??? ? super.downloadFile(path, StrutsUtil.getResponse());
??? ??? ?
??? ??? ? int id = (Integer.parseInt(StrutsUtil.getParameter("id")));
??? ??? ? super.getAccessoryDao().updateDownloadCount(id);
??? ? }