Java项目中的下载 与 上传

Posted Advancing Swift

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java项目中的下载 与 上传相关的知识,希望对你有一定的参考价值。

使用超级链接下载,一般会在浏览器中直接打开,而不是出现下载框

如果要确保出现下载框下载文件,则需要设置response中的参数:

1是要设置用附件的方式下载

Content-Disposition: attachment; filename =

2要设定内容的MIME类型

Content-Type:application/x-msdownload

下载的页面代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  <h1>服务器的文件资源 点击下载</h1>
  <a href="/down/download/a.flv">a.flv</a><br>
  <a href="/down/download/a.jpg">a.jpg</a><br>
  <a href="/down/download/a.mp3">a.mp3</a><br>
  <a href="/down/download/a.mp4">a.mp4</a><br>
  <a href="/down/download/a.txt">a.txt</a><br>
  <a href="/down/download/a.zip">a.zip</a><br>
  
   <h1>肯定下载</h1>
   <a href="/down/down?filename=a.jpg">美女.jpg</a><br>
 
</body>
</html>

原始servlet

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Encoder;

/**
 * 文件下载的Servlet
 */
public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.接收参数
        String filename = new String(request.getParameter("filename"));
        System.out.println(filename);
        // 2.完成文件下载:
        // 2.1设置Content-Type头
        String type = this.getServletContext().getMimeType(filename);
        response.setHeader("Content-Type", type);
        // 2.3设置文件的InputStream.
        String realPath = this.getServletContext().getRealPath("/download/"+filename);
        
        // 根据浏览器的类型处理中文文件的乱码问题:
        String agent = request.getHeader("User-Agent");
        System.out.println(agent);
        if(agent.contains("Firefox")){
            filename = base64EncodeFileName(filename);
        }else{
            filename = URLEncoder.encode(filename,"UTF-8");
        }
        
        // 2.2设置Content-Disposition头
        response.setHeader("Content-Disposition", "attachment;filename="+filename);
        
        InputStream is = new FileInputStream(realPath);
        // 获得response的输出流:
        OutputStream os = response.getOutputStream();
        int len = 0;
        byte[] b = new byte[1024];
        while((len = is.read(b))!= -1){
            os.write(b, 0, len);
        }
        is.close();
    }
    
    public static String base64EncodeFileName(String fileName) {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        try {
            return "=?UTF-8?B?"
                    + new String(base64Encoder.encode(fileName
                            .getBytes("UTF-8"))) + "?=";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

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

}

检查下web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>down</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>DownloadServlet</display-name>
    <servlet-name>DownloadServlet</servlet-name>
    <servlet-class>DownloadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DownloadServlet</servlet-name>
    <url-pattern>/down</url-pattern>
  </servlet-mapping>
</web-app>

 

原始上传方式要有上传按钮 提交按钮及表单才行

相比之下ocupload(即 one-click-upload 一键上传)要简单的多,只需一个按钮

一键上传的upload()方法需要ocupload的js支持 jquery.ocupload-1.1.2.js

页面上需要引入

<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>

<script type="text/javascript" src="${pageContext.request.contextPath }/js/ocupload/jquery.ocupload-1.1.2.js"></script>

上传的页面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/ocupload/jquery.ocupload-1.1.2.js"></script>
</head>
<body>
//文件上传页面三个要素  :<br>
1、表单提交方式必须post  <br>
2、设置表单属性encty="multipart/form-data"  <br> 
3、在表单中存在input type="file" 必须有name<br>
<iframe name="aaaa" style="display: none;"></iframe>
<form target="aaaa" action="abc.action" method="post" enctype="multipart/form-data">
    <input type="file" name="upload" value="选择文件">
    <input type="submit" value="上传">
</form>

<hr>
使用ocupload实现文件上传:原理调用upload方法动态修改html元素<br>
1、在页面中提供任意元素 给出Id<br>
2、在页面加载完成后调用upload方法<br>
<input type="button" id="myBtn" value="上传">
<script type="text/javascript">
    $(function(){
        $("#myBtn").upload({
            action:xyz.action,  //提交url
            name:upload         //提交文件参数名
        });
    })
</script>
</body>
</html>

 

 


 

如何解决上传不用刷新问题?

使用<iframe>标签,把表单的target指向iframe,再把iframe隐藏即可


 

以上是关于Java项目中的下载 与 上传的主要内容,如果未能解决你的问题,请参考以下文章

java Ftp上传创建多层文件的代码片段

springboot项目关于文件的上传与下载

用SonarQube和SonarLint分析maven项目中的Java代码坏味道

用SonarQube和SonarLint分析maven项目中的Java代码坏味道

用java实现文件的上传与下载

SpringBoot.08.SpringBoot文件上传与下载