java文件導出下載,怎樣導出java文件

本文目錄一覽:

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();

  }

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/252109.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-13 17:34
下一篇 2024-12-13 17:34

相關推薦

發表回復

登錄後才能評論