生活随笔
收集整理的這篇文章主要介紹了
jsp中简易版本的图片上传程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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.具體調用代碼如下:
3.1 上傳頁面代碼:
//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>
<%@ 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()+"/";
// 文件夾不存在就自動創(chuàng)建:if(!new File(uploadPath).isDirectory())new File(uploadPath).mkdirs();if(!new File(tempPath).isDirectory())new File(tempPath).mkdirs();try {DiskFileUpload fu = new DiskFileUpload();
// 設置最大文件尺寸,這里是4MBfu.setSizeMax(4194304);
// 設置緩沖區(qū)大小,這里是4kbfu.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中简易版本的图片上传程序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。