博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中的上传下载----ajaxFileUpload+struts2
阅读量:4476 次
发布时间:2019-06-08

本文共 5001 字,大约阅读时间需要 16 分钟。

文件上传在项目中应该是非常常见的,而且很多时候,上传文件都只是一个小页面中的一个功能,要求在实现文件上传的前提下不刷新页面。而一般情况下将客户端的文件包装成网络地址传递到服务器端然后通过流来进行文件传输的任务都是使用浏览器来帮我们完成的,一般情况下,我们的form表单提交,我们自己可以手动拿到表单的值,然后封装起来,发送ajax请求,为了安全着想,js是不允许访问客户端的文件系统的,所以而文件传输需要浏览器来完成,ajax上传其实是在页面中一小块区域加一个iframe对象然后引用到另一个页面,提交引用的那个页面。

1、ajaxFileUpload文件下载地址  

2、

jsp页面 (<input type="file"/>默认的样式很丑,调整样式有点小麻烦)

我没有加表单,ajax提交表单应该没什么问题,方法很多网上百度

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>      My JSP 'index.jsp' starting page                                                    

3、action

package com.demo.action;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class FileAction extends ActionSupport {    private File file;    private String fileFileName;    private String fileFileContentType;    private String message = "0"; // 0格式错误 1成功(文件路径)  2失败    private String filePath;    public String getFilePath() {        return filePath;    }    public void setFilePath(String filePath) {        this.filePath = filePath;    }    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }    public File getFile() {        return file;    }    public void setFile(File file) {        this.file = file;    }    public String getFileFileName() {        return fileFileName;    }    public void setFileFileName(String fileFileName) {        this.fileFileName = fileFileName;    }    public String getFileFileContentType() {        return fileFileContentType;    }    public void setFileFileContentType(String fileFileContentType) {        this.fileFileContentType = fileFileContentType;    }    @SuppressWarnings("deprecation")    @Override    public String execute() throws Exception {        String path = ServletActionContext.getRequest().getRealPath("/upload");        File file = new File(path); // 判断文件夹是否存在,如果不存在则创建文件夹        if (!file.exists()) {            file.mkdir();        }        String[] fileSuffix = new String[] { ".exe" };// 禁止文件        try {            File f = this.getFile();            // 判断文件格式            for (int i = 0; i < fileSuffix.length; i++) {                if (fileFileName.endsWith(fileSuffix[i])) {                    message = "0";                    return ERROR;                }            }            FileInputStream inputStream = new FileInputStream(f);            FileOutputStream outputStream = new FileOutputStream(path + "\\"                    + fileFileName);            byte[] buf = new byte[1024];            int length = 0;            while ((length = inputStream.read(buf)) != -1) {                outputStream.write(buf, 0, length);            }            inputStream.close();            outputStream.flush();            message = path + "\\" + this.getFileFileName();        } catch (Exception e) {            e.printStackTrace();            message = "2";        }        return SUCCESS;    }    public String download() {        String path = filePath;        HttpServletResponse response = ServletActionContext.getResponse();        try {            // path是指欲下载的文件的路径。            File file = new File(path);            // 取得文件名。            String filename = file.getName();            // 取得文件的后缀名。            String ext = filename.substring(filename.lastIndexOf(".") + 1)                    .toUpperCase();            // 以流的形式下载文件。            InputStream fis = new BufferedInputStream(new FileInputStream(path));            byte[] buffer = new byte[fis.available()];            fis.read(buffer);            fis.close();            // 清空response            response.reset();            // 设置response的Header            String filenameString = new String(filename.getBytes("gbk"),                    "iso-8859-1");            response.addHeader("Content-Disposition", "attachment;filename="                    + filenameString);            response.addHeader("Content-Length", "" + file.length());            OutputStream toClient = new BufferedOutputStream(response                    .getOutputStream());            response.setContentType("application/octet-stream");            toClient.write(buffer);            toClient.flush();            toClient.close();        } catch (IOException ex) {            ex.printStackTrace();        }        return null;    }}

4、struts2配置

text/html
text/html

 

为生活,一路向前

转载于:https://www.cnblogs.com/snake-hand/p/3161582.html

你可能感兴趣的文章
两个整数,求他们的最小公倍数和最大公约数
查看>>
Mongo索引
查看>>
php 实现设计模式之 建造者模式
查看>>
An Easy C Program Problem
查看>>
Replace Nested Conditional with Guard Clauses(用卫语句代替嵌套循环)
查看>>
jsp中${}是EL表达式的常规表示方式
查看>>
GoldenGate常见问题及处理
查看>>
Android JNI学习(五)——Demo演示
查看>>
SSRS 呈现Barcode Free
查看>>
java快速排序引起的StackOverflowError异常
查看>>
泛函编程(35)-泛函Stream IO:IO处理过程-IO Process
查看>>
-XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
查看>>
mac 安装php redis扩展
查看>>
css3中Animation
查看>>
JS 判断是否是手机端并跳转操作
查看>>
最短路径问题(dijkstra-模板)
查看>>
c# 导出表格 api
查看>>
使用Android NDK以及JNI编写应用
查看>>
学习笔记之-php数组数据结构
查看>>
初学者--bootstrap(六)组件中的下拉菜单----在路上(10)
查看>>