首页 > 编程知识 正文

java文件导出下载,怎样导出java文件

时间:2023-12-28 11:57:09 阅读:328377 作者:FDFY

本文目录一览:

Java文件的导入导出!(高手进)

最简单的方法:

上传文件到服务器(任何文件格式都行),将该文件路径存入数据库.点该文件下载时,从服务器路径找该文件,读流,读格式,生成文件.

上传下载这个总会吧?

java下载服务器上的文件到客户端

java编程方法下载服务器上的文件到本地客服端,代码如下:

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.net.URLConnection;

 

public class DownLoad {   

 public static void downloadFile(URL theURL, String filePath) throws IOException {  

   File dirFile = new File(filePath);

      if(!dirFile.exists()){ 

        //文件路径不存在时,自动创建目录

        dirFile.mkdir();

      }

  //从服务器上获取图片并保存

     URLConnection connection = theURL.openConnection();

     InputStream in = connection.getInputStream();  

     FileOutputStream os = new FileOutputStream(filePath+"\123.png"); 

     byte[] buffer = new byte[4 * 1024];  

     int read;  

     while ((read = in.read(buffer))  0) {  

        os.write(buffer, 0, read);  

          }  

       os.close();  

       in.close();

  }   

     public static void main(String[] args) { 

      //下面添加服务器的IP地址和端口,以及要下载的文件路径

      String urlPath = "http://服务器IP地址:端口/image/123.png"; 

      

      //下面代码是下载到本地的位置

      String filePath = "d:\excel"; 

  

      URL url = new URL(urlPath); 

  

          try { 

  

             downloadFile(url,filePath); 

  

           } catch (IOException e) { 

  

            e.printStackTrace(); 

  

         } 

  

      }   

}

java导出word文档后,怎么在客户端提供下载。。。

做成一套吧。

1、上传功能。将上传的文件保存到服务器上,同时将服务器上文件的路径,文件名等等数据存到数据库中。

2、显示数据库中保存的文件列表。

3、选择对应的文件点击下载,后台到数据库中读取路径,然后处理请求。

java : 一个excel文件以二进制的形式存在数据库中 如何将它导出并下载到本地

从数据库中得到Blob/Clob,然后得到InputStream,直接给response.getOutputStream() 输出就可以

java如何将导出的excel下载到客户端

package com.mr;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

 * 利用Servlet导出Excel

 * @author CHUNBIN

 *

 */

public class ExportExcelServlet extends HttpServlet {

      

       public void doGet(HttpServletRequest request, HttpServletResponse response)

                     throws ServletException, IOException {

              doPost(request, response);

       }

 

       public void doPost(HttpServletRequest request, HttpServletResponse response)

                     throws ServletException, IOException {

              request.setCharacterEncoding("UTF-8");//设置request的编码方式,防止中文乱码

              String fileName ="导出数据";//设置导出的文件名称

              StringBuffer sb = new StringBuffer(request.getParameter("tableInfo"));//将表格信息放入内存

              String contentType = "application/vnd.ms-excel";//定义导出文件的格式的字符串

              String recommendedName = new String(fileName.getBytes(),"iso_8859_1");//设置文件名称的编码格式

              response.setContentType(contentType);//设置导出文件格式

              response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + """);//

              response.resetBuffer();

              //利用输出输入流导出文件

              ServletOutputStream sos = response.getOutputStream();

              sos.write(sb.toString().getBytes());

              sos.flush();

              sos.close();

       }

}

%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=UTF-8"

title导出Excel/title

script type="text/javascript"

    function test(){

       document.getElementById("tableInfo").value=document.getElementById("table").innerHTML;

    }

/script

style

    body{font-family:宋体;font-size:11pt}

/style

/head

body

form action="%=request.getContextPath()%/servlet/ExportExcelServlet" method="post"

    span id="table"

    table bgcolor="#EEECF2" bordercolor="#A3B2CC" border="1" cellspacing="0"

       trth学号/thth姓名/thth科目/thth分数/th/tr

       trtd10001/tdtd赵二/tdtd高数/tdtd82/td/tr

       trtd10002/tdtd张三/tdtd高数/tdtd94/td/tr

       trtd10001/tdtd赵二/tdtd线数/tdtd77/td/tr

       trtd10002/tdtd张三/tdtd线数/tdtd61/td/tr

    /table

    /spanbr/

    input type="submit" name="Excel" value="导出表格" onclick="test()"/

    input type="hidden" id="tableInfo" name="tableInfo" value=""/

/form

/body

/html

以上代码来自网络:

JAVA 上传下载文件

Java代码实现文件上传

  FormFile file=manform.getFile(); 

  String newfileName = null;

  String newpathname=null;

  String fileAddre="/numUp";

  try {

   InputStream stream = file.getInputStream();// 把文件读入

    String filePath = request.getRealPath(fileAddre);//取系统当前路径

          File file1 = new File(filePath);//添加了自动创建目录的功能

       ((File) file1).mkdir();   

    newfileName = System.currentTimeMillis()

     + file.getFileName().substring(

       file.getFileName().lastIndexOf('.'));

   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   OutputStream bos = new FileOutputStream(filePath + "/"

     + newfileName);

   newpathname=filePath+"/"+newfileName;

   System.out.println(newpathname);

   // 建立一个上传文件的输出流

    System.out.println(filePath+"/"+file.getFileName());

   int bytesRead = 0;

   byte[] buffer = new byte[8192];

   while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {

    bos.write(buffer, 0, bytesRead);// 将文件写入服务器

   }

   bos.close();

   stream.close();

    } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。