当前位置: 代码迷 >> Java Web开发 >> 求Struts2文件上传下载源码。该如何解决
  详细解决方案

求Struts2文件上传下载源码。该如何解决

热度:105   发布时间:2016-04-17 01:06:37.0
求Struts2文件上传下载源码。
求Struts2文件上传下载源码。(包括文件说明)
最好有说明。网上教程虽多,不过不放心用。
急用。谢谢。
麻烦顺手帖下代码。谢谢~

------解决方案--------------------
我们用jspsmart
------解决方案--------------------
我刚在资源里面上传了整个上传下载的项目,是我之前单独实验的,你可以看看,可以多文件上传的
------解决方案--------------------
下载

Java code
 public void downloadFile() {        try {            HttpServletResponse response = ServletActionContext.getResponse();            HttpServletRequest request = ServletActionContext.getRequest();            String webHost = request.getContextPath();            handbookUrl = new String(handbookUrl.getBytes("ISO-8859-1"), "UTF-8");            String filePath = handbookUrl;            filePath = request.getRealPath(filePath);            download(filePath, response);        } catch (Exception e) {            e.printStackTrace();        }    }    public HttpServletResponse download(String path, HttpServletResponse response) {        try {            // path是指欲下载的文件的路径。            File file = new File(path);            // 取得文件名。            String filename = file.getName();            // 以流的形式下载文件。            InputStream fis = new BufferedInputStream(new FileInputStream(path));            // 清空response            response.reset();            // 设置response的Header            response.setBufferSize(5 * 1024 * 1024);            filename = new String(filename.getBytes("GBK"), "ISO8859_1");            response.addHeader("Content-Disposition", "attachment;filename=" + filename);            response.addHeader("Content-Length", "" + file.length());            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());            response.setContentType("application/octet-stream");            int readLength = 0;            byte[] readUnit = new byte[1024 * 1024];            while ((readLength = fis.read(readUnit)) != -1) {                toClient.write(readUnit, 0, readLength);                toClient.flush();            }            fis.close();            toClient.flush();            toClient.close();        } catch (IOException ex) {            ex.printStackTrace();        }        return response;    }
------解决方案--------------------
http://www.vaannila.com/struts-2/struts-2-example/struts-2-file-Upload-example-1.html

You can download the Struts 2 File Upolad example by clicking the Download link below.
Source :Download
War :Download
------解决方案--------------------

上传
Java code
/*     * 上传文件     */    public String uploadFile(File upload, String uploadFileName,            String directory) {        String path = "upload";        String filePath = "";        try {            HttpServletRequest request = ServletActionContext.getRequest();            String fileName = uploadFileName;            // 获取物理路径            String sPath = request.getRealPath(path+File.separator + directory)+File.separator;            // 获取网络地址            filePath = path + "/" + directory                    + "/" + fileName;            String outputFileName = sPath + fileName;            File outputPathFile = new File(sPath);            if (!outputPathFile.exists()) {                outputPathFile.mkdirs();            }            // 保存文件            File outputFile = new File(outputFileName);            java.io.InputStream is = new FileInputStream(upload);            java.io.OutputStream os = new java.io.FileOutputStream(outputFile);            if (outputFile.exists())                outputFile.delete();            byte buffer[] = new byte[8192];            int count = 0;            while ((count = is.read(buffer)) > 0) {                os.write(buffer, 0, count);            }            os.close();            is.close();        } catch (Exception e) {            e.printStackTrace();        }        return filePath;    }
  相关解决方案