濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > jsp+servlet實(shí)現(xiàn)文件上傳與下載功能

jsp+servlet實(shí)現(xiàn)文件上傳與下載功能

熱門(mén)標(biāo)簽:電銷(xiāo)機(jī)器人免培訓(xùn) 潤(rùn)滑油銷(xiāo)售電銷(xiāo)機(jī)器人 南通通訊外呼系統(tǒng)產(chǎn)品介紹 如何看懂地圖標(biāo)注點(diǎn) 外呼系統(tǒng)使用方法 電話(huà)機(jī)器人需要使用網(wǎng)絡(luò)嗎 給地圖標(biāo)注得傭金 自繪地圖標(biāo)注數(shù)據(jù) 海外圖書(shū)館地圖標(biāo)注點(diǎn)

本文實(shí)例為大家分享了jsp servlet實(shí)現(xiàn)文件上傳與下載的具體代碼,供大家參考,具體內(nèi)容如下

上傳:

需要導(dǎo)入兩個(gè)包:commons-fileupload-1.2.1.jar,commons-io-1.4.jar

import java.io.File;
import java.io.IOException;
import java.util.List;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * 上傳附件
 * @author new
 *
 */
public class UploadAnnexServlet extends HttpServlet {

 private static String path = "";

 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 doPost(request, response);
 }

 /*
 * post處理
 * (non-Javadoc)
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 path = this.getServletContext().getRealPath("/upload");

 try {
 DiskFileItemFactory factory = new DiskFileItemFactory();
 ServletFileUpload up = new ServletFileUpload(factory);
 ListFileItem> ls = up.parseRequest(request);

 for (FileItem fileItem : ls) {
 if (fileItem.isFormField()) {
  String FieldName = fileItem.getFieldName();
  //getName()返回的是文件名字 普通域沒(méi)有文件 返回NULL
  //     String Name = fileItem.getName();
  String Content = fileItem.getString("gbk");
  request.setAttribute(FieldName, Content);
 } else {
 
  String nm = fileItem.getName().substring(
  fileItem.getName().lastIndexOf("\\") + 1);
  File mkr = new File(path, nm);
  if (mkr.createNewFile()) {
  fileItem.write(mkr);//非常方便的方法
  }
  request.setAttribute("result", "上傳文件成功!");
 }
 }
 } catch (Exception e) {
 e.printStackTrace();
 request.setAttribute("result", "上傳失敗,請(qǐng)查找原因,重新再試!");
 }
 request.getRequestDispatcher("/pages/admin/annex-manager.jsp").forward(
 request, response);
 }

}

下載(i/o流)無(wú)需導(dǎo)包:

import java.io.IOException;
import java.net.URLEncoder;

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

/**
 * 下載文件
 * @author
 *
 */
public class DownloadFilesServlet extends HttpServlet {

 /**
 * 
 */
 private static final long serialVersionUID = 8594448765428224944L;

 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 doPost(request, response);
 }

 /*
 * 處理請(qǐng)求
 * (non-Javadoc)
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 String name = request.getParameter("fileName");

 System.out.print("dddddddddd:" + name);
 // web絕對(duì)路徑
 String path = request.getSession().getServletContext().getRealPath("/");
 String savePath = path + "upload";

 // 設(shè)置為下載application/x-download
 response.setContentType("application/x-download");
 // 即將下載的文件在服務(wù)器上的絕對(duì)路徑
 String filenamedownload = savePath + "/" + name;
 // 下載文件時(shí)顯示的文件保存名稱(chēng)
 String filenamedisplay = name;
 // 中文編碼轉(zhuǎn)換
 filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");
 response.addHeader("Content-Disposition", "attachment;filename="
 + filenamedisplay);
 try {
 java.io.OutputStream os = response.getOutputStream();
 java.io.FileInputStream fis = new java.io.FileInputStream(
  filenamedownload);
 byte[] b = new byte[1024];
 int i = 0;
 while ((i = fis.read(b)) > 0) {
 os.write(b, 0, i);
 }
 fis.close();
 os.flush();
 os.close();
 } catch (Exception e) {

 }

 }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)
  • Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)
  • Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件上傳(一)
  • JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
  • servlet+JSP+mysql實(shí)現(xiàn)文件上傳的方法
  • JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能

標(biāo)簽:銅川 廣州 南京 大連 內(nèi)江 貸款邀約 黃石 樂(lè)山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp+servlet實(shí)現(xiàn)文件上傳與下載功能》,本文關(guān)鍵詞  jsp+servlet,實(shí)現(xiàn),文件,上傳,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《jsp+servlet實(shí)現(xiàn)文件上傳與下載功能》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于jsp+servlet實(shí)現(xiàn)文件上傳與下載功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    丰镇市| 邵东县| 保康县| 墨江| 台江县| 措美县| 察隅县| 东莞市| 龙井市| 乌鲁木齐县| 明光市| 浮梁县| 米林县| 阜新市| 什邡市| 句容市| 黑龙江省| 渭源县| 茌平县| 甘南县| 扶风县| 如东县| 武强县| 福清市| 茶陵县| 沙田区| 乐东| 淳化县| 长兴县| 阿鲁科尔沁旗| 桓台县| 隆回县| 河曲县| 香港| 安顺市| 浦城县| 台中市| 岢岚县| 宜春市| 昌平区| 都江堰市|