首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

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

2012-01-16 
求Struts2文件上传下载源码。求Struts2文件上传下载源码。(包括文件说明)最好有说明。网上教程虽多,不过不放

求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;    } 


[解决办法]
如果Struts 2配置好,可以这样

Action

Java code
public class FileUpload extends ActionSupport {    /**     *      */    private static final long serialVersionUID = 1664491233055815865L;    private File file;    @Override    public String execute() throws Exception {        System.out.println(file);        addActionMessage("上传文件服务器路径为:" + file.toString());//上传成功后,这里有上传的文件对象(File),可以直接访问,只不过文件被保存为.tmp                return Action.SUCCESS;    }    public File getFile() {        return file;    }    public void setFile(File file) {        this.file = file;    }} 

热点排行