怎么进行javaweb文件上传

这篇文章的内容主要围绕怎么进行java web 文件上传进行讲述,文章内容清晰易懂,条理清晰,非常适合新手学习,值得大家去阅读。感兴趣的朋友可以跟随小编一起阅读吧。希望大家通过这篇文章有所收获!

创新互联建站服务项目包括共青城网站建设、共青城网站制作、共青城网页制作以及共青城网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,共青城网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到共青城省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

java web 文件上传

文件资源位置:web/img下
HTML页面



    
    学习文件下载


    

使用超链接

    图片1     视频1     图片2
    图片1     视频1     中文图片2
Servlet 类
@WebServlet("/downServlet")
public class FileDownServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //获取请求参数,文件名称
        String filename = request.getParameter("filename");
        ServletContext context = this.getServletContext();
        //找到服务器文件的真实路径
        String realPath = context.getRealPath("/img/" + filename);
        //使用字节流关联
        FileInputStream fis=new FileInputStream(realPath);
        /* 设置响应头
            响应头类型:content-type
            响应头打开方式:content-disposition
         */
        String mimeType = context.getMimeType(filename);
        response.setHeader("content-type",mimeType);
      //获取user-agent请求头
        String agent = request.getHeader("user-agent");
      //使用工具类方法编码文件名即可
        filename = DownLoadUtils.getFileName(agent, filename);

        response.setHeader("content-disposition","attachment;filename="+filename);
      //将输入流的数据写出到输出流中
        ServletOutputStream sos = response.getOutputStream();
        byte[] bytes=new byte[1024*8];
        int len=0;
        while ((len=fis.read(bytes))!=-1){
            sos.write(bytes,0,len);
        }
        fis.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

util工具类:对文件名编码
public class DownLoadUtils {

    public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
        if (agent.contains("MSIE")) {
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            // 火狐浏览器
            BASE64Encoder base64Encoder = new BASE64Encoder();
            filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
        } else {
            // 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }
}

Java的特点有哪些

Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

感谢你的阅读,相信你对“怎么进行java web 文件上传”这一问题有一定的了解,快去动手实践吧,如果想了解更多相关知识点,可以关注创新互联网站!小编会继续为大家带来更好的文章!


分享名称:怎么进行javaweb文件上传
文章位置:http://csdahua.cn/article/ihpijg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流