国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Spring3.1 文件上傳

注意:以下上傳和下載方法未必完全正確,不同瀏覽器效果不同,建議不要使用IE

/**
  * 簡(jiǎn)單的文件上傳
  * @author:qiuchen
  * @createTime:2012-6-19
  * @param request
  * @param response
  * @param errors
  * @return
  * @throws Exception
  */
 @RequestMapping(value = "/upload", method = RequestMethod.POST)
 public ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response, BindException errors) throws Exception {
  //上傳文件處理器
  MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  //文件對(duì)象
  CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
  
  //從表單域中獲取文件對(duì)象的詳細(xì),如:文件名稱等等
  String name = multipartRequest.getParameter("name");
  System.out.println("name: " + name);
  
  // 獲得文件名(全路徑)
  String realFileName = file.getOriginalFilename();
  realFileName = encodeFilename(realFileName,request);
  System.out.println("獲得文件名:" + realFileName);

  // 獲取當(dāng)前web服務(wù)器項(xiàng)目路徑
  String ctxPath = request.getSession().getServletContext().getRealPath("/")+ "fileupload/";
  
  // 創(chuàng)建文件夾
  File dirPath = new File(ctxPath);
  if (!dirPath.exists()) {
   dirPath.mkdir();
  }
  //創(chuàng)建文件
  File uploadFile = new File(ctxPath + realFileName);
  
  //Copy文件
  FileCopyUtils.copy(file.getBytes(), uploadFile);
  
   return new ModelAndView("success");
 }

 

/**
  * 批量上傳文件
  * @author:qiuchen
  * @createTime:2012-6-19
  * @param request
  * @param response
  * @param errors
  * @return
  * @throws Exception
  */
 @RequestMapping(value = "/upload2", method = RequestMethod.POST)
 public ModelAndView onSubmit2(HttpServletRequest request,HttpServletResponse response, BindException errors) throws Exception {
  //文件處理器
  MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  //文件列表
  Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  //獲取服務(wù)器上傳文件夾地址
  String ctxPath = request.getSession().getServletContext().getRealPath("/")+ "\\" + "fileupload\\";
  //創(chuàng)建文件夾
  File file = new File(ctxPath);
  if (!file.exists()) {
   file.mkdir();
  }
  
  String fileName = null;
  for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
   //單個(gè)文件
   MultipartFile mf = entity.getValue();
   //文件名
   fileName = mf.getOriginalFilename();
   //創(chuàng)建文件
   File uploadFile = new File(ctxPath + fileName);
   //copy從內(nèi)存中復(fù)制到磁盤(pán)上
   FileCopyUtils.copy(mf.getBytes(), uploadFile);
  }
    return new ModelAndView("success");
 }

 

/** 
     * 設(shè)置下載文件中文件的名稱
     * @param filename 
     * @param request 
     * @return 
     */   
 public static String encodeFilename(String filename,HttpServletRequest request) {
  /**
   * 獲取客戶端瀏覽器和操作系統(tǒng)信息 在IE瀏覽器中得到的是:User-Agent=Mozilla/4.0 (compatible; MSIE
   * 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
   * 在Firefox中得到的是:User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1;
   * zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
   */
  String agent = request.getHeader("USER-AGENT");
  try {
   if ((agent != null) && (-1 != agent.indexOf("MSIE"))) { // IE瀏覽器
    String newFileName = URLEncoder.encode(filename, "UTF-8");
    newFileName = StringUtils.replace(newFileName, "+", "%20");
    if (newFileName.length() > 150) {
     newFileName = new String(filename.getBytes("GB2312"),
       "ISO8859-1");
     newFileName = StringUtils.replace(newFileName, " ", "%20");
    }
    return newFileName;
   }
   if ((agent != null) && (-1 != agent.indexOf("Mozilla"))) // 火狐瀏覽器
    return MimeUtility.encodeText(filename, "UTF-8", "B");
   return filename;
  } catch (Exception ex) {
   return filename;
  }
 }

 

/**
  * 文件下載
  * @author:qiuchen
  * @createTime:2012-6-19
  * @param fileName
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 @RequestMapping("/download/{fileName}")
 public ModelAndView download(@PathVariable("fileName") String fileName,HttpServletRequest request, HttpServletResponse response)throws Exception {
  request.setCharacterEncoding("UTF-8");
  
  BufferedInputStream bis = null;  //從文件中讀取內(nèi)容
  BufferedOutputStream bos = null; //向文件中寫(xiě)入內(nèi)容
  
  //獲得服務(wù)器上存放下載資源的地址
  String ctxPath = request.getSession().getServletContext().getRealPath("/")+ "\\" + "fileupload\\";
  //獲得下載文件全路徑
  String downLoadPath = ctxPath + fileName;
  System.out.println(downLoadPath);
  //如果文件不存在,退出
  File file = new File(downLoadPath);
  if(!file.exists()){
   return null;
  }
  try {
   //獲得文件大小
   long fileLength = new File(downLoadPath).length();
   System.out.println(new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
   response.setContentType("text/html;charset=utf-8"); //設(shè)置相應(yīng)類型和編碼
   response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
   response.setHeader("Content-Length", String.valueOf(fileLength));
   
   bis = new BufferedInputStream(new FileInputStream(downLoadPath));
   bos = new BufferedOutputStream(response.getOutputStream());
   //定義文件讀取緩沖區(qū)
   byte[] buff = new byte[2048];
   int bytesRead;
   //把下載資源寫(xiě)入到輸出流
   while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff, 0, bytesRead);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (bis != null)
    bis.close();
   if (bos != null)
    bos.close();
  }
  return null;
 }

 

對(duì)于正在復(fù)制代碼的你,Control+O導(dǎo)入命名包時(shí),以下應(yīng)該是你喜歡的:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.BindException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
asp.net上傳execl文件后,在頁(yè)面上加載顯示。
Java 項(xiàng)目生成靜態(tài)頁(yè)面的代碼
[原創(chuàng)]JAVA讀取文件或是數(shù)據(jù)流的源代碼--涵蓋了多種形式
jar文件替換
基于SMB/JCIFS協(xié)議的共享文件上傳和下載
FTPClient 用法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服