JSP上傳圖片程式

來源:互聯網
上載者:User

標籤:des   style   http   io   ar   color   os   sp   for   

1.下載相應的組件的最新版本

Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下載

附加的Commons IO  可以在http://jakarta.apache.org/commons/io/下載

2.將commons-fileupload-1.2.1.jar commons-io-1.4.jar拷貝到$TOMCAT\common\lib目錄下

3.編寫相應的代碼如下

 

UploadExample.jsp

 

<%@ page contentType=‘text/html;charset=gb2312‘%>
<html>
<title><%= application.getServerInfo() %></title>
<body>

上傳檔案程式應用樣本
<form action=‘doUpload.jsp‘ method=‘post‘ enctype=‘multipart/form-data‘>

<%-- 類型enctype用multipart/form-data,這樣可以把檔案中的資料作為流式資料上傳,不管是什麼檔案類型,均可上傳。--%>
請選擇要上傳的檔案<input type=‘file‘ name=‘upfile‘ size=‘50‘>
<input type=‘submit‘ value=‘提交‘>
</form>
</body>
</html>

 

 doUpload.jsp

 

<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>

<% response.setContentType("text/html");
//   圖片上傳路徑
   String uploadPath =request.getSession().getServletContext().getRealPath("/")+"upload/images/";
//   圖片臨時上傳路徑
   String tempPath = request.getSession().getServletContext().getRealPath("/")+"upload/images/temp/";
//   圖片網路相對路徑
   String imagePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; 
//   檔案夾不存在就自動建立:
   if(!new File(uploadPath).isDirectory())
   new File(uploadPath).mkdirs();
   if(!new File(tempPath).isDirectory())
   new File(tempPath).mkdirs();
   try {
   DiskFileUpload fu = new DiskFileUpload();
//   設定最大檔案尺寸,這裡是4MB
   fu.setSizeMax(4194304);
//   設定緩衝區大小,這裡是4kb
   fu.setSizeThreshold(4096);
//   設定臨時目錄:
   fu.setRepositoryPath(tempPath);
//   得到所有的檔案:
   List fileItems = fu.parseRequest(request);
   Iterator i = fileItems.iterator();
//   依次處理每一個檔案:
   while(i.hasNext()) {
   FileItem file = (FileItem)i.next();
//   獲得檔案名稱,這個檔案名稱是使用者上傳時使用者的絕對路徑:
   String sourcefileName = file.getName();
   if(sourcefileName!=null&&(sourcefileName.endsWith(".jpg")||sourcefileName.endsWith(".gif"))) {
//   在這裡可以記錄使用者和檔案資訊,產生上傳後的檔案名稱
   String destinationfileName=null;
   Random rd = new Random();
   Calendar time = Calendar.getInstance();
   if(sourcefileName.endsWith(".jpg")){
   destinationfileName=String.valueOf(time.get(Calendar.YEAR))
   + String.valueOf(time.get(Calendar.MONTH))
   + String.valueOf(time.get(Calendar.DAY_OF_MONTH))
   + String.valueOf(time.get(Calendar.HOUR_OF_DAY))
   + String.valueOf(time.get(Calendar.MINUTE))
   + String.valueOf(time.get(Calendar.SECOND))
   + String.valueOf(rd.nextInt(100)) + ".jpg";
   }else if(sourcefileName.endsWith(".gif")){
   destinationfileName=String.valueOf(time.get(Calendar.YEAR))
   + String.valueOf(time.get(Calendar.MONTH))
   + String.valueOf(time.get(Calendar.DAY_OF_MONTH))
   + String.valueOf(time.get(Calendar.HOUR_OF_DAY))
   + String.valueOf(time.get(Calendar.MINUTE))
   + String.valueOf(time.get(Calendar.SECOND))
   + String.valueOf(rd.nextInt(100)) + ".gif";
   }
   File f1=new File(uploadPath+ destinationfileName);
   file.write(f1);
   out.print(sourcefileName+"成功上傳!") ;
   out.print("<img src="+imagePath+"upload/images/"+destinationfileName+">");
   }else{
   out.println("上傳檔案出錯,只能上傳 *.jpg , *.gif");
   }
   }
//   跳轉到上傳成功提示頁面
   }
   catch(Exception e) {
//   可以跳轉出錯頁面
   }
   out.flush();
   out.close();

%>

JSP上傳圖片程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.