java 代码
- <%@ include file="include/include_clear.jsp"%>
- <%@ page contentType="text/html; charset=GB18030" language="java"%>
- <%@ page import="java.io.*"%>
- <%@ page import="java.util.*"%>
- <%
- String DOCROOT = "/home/httpd/pdb";
- String UPDIR = "/upload";
- String fileName = request.getParameter("load");
- String fileExt = request.getParameter("load.filename");//取到文件类型
- FileInputStream source = null;
- FileOutputStream destination = null;
- String ext = "";
- String exts[] = {"jpg","gif"};
- int flag = 1;//初始判断为假(不符合文件类型的)
- String destfile = "";
- out.println(fileExt);
- if(fileExt.lastIndexOf(46) > 0){
- int start = fileExt.lastIndexOf(46) + 1; //判断"."
- ext = fileExt.substring(start, fileExt.length()).toLowerCase();//转换大写JSP/GIF
- }
- for(int i=0;i<exts.length;i++){
- if(exts[i].equals(ext)){
- flag = 0;//符合文件类型
- break;
- }
- }
- if(flag==0){
- int bytes_read = 0;
- try{
- source = new FileInputStream(fileName);
- Calendar now = Calendar.getInstance();
- destfile = UPDIR+"/" +now.get(Calendar.YEAR)+(1+now.get(Calendar.MONTH))+ "/"+System.currentTimeMillis()+"."+ext;//取年月时间生成相应目录
- String dir = DOCROOT+destfile.substring(0,destfile.lastIndexOf("/"));
- File Fdir = new File(dir);
- if (!Fdir.isDirectory()) {Fdir.mkdir();}//根据取到的年月时间生成相应目录
- out.println(destfile);
- destination = new FileOutputStream(DOCROOT+destfile);
- byte[] buffer = new byte[1024];
- for(;;){
- bytes_read=source.read(buffer);
- if(bytes_read==-1) break;
- destination.write(buffer,0,bytes_read);
- }
- }catch(Exception e){
- out.println(e);
- e.printStackTrace();
- }finally {
- if (source!=null) source.close();
- if (destination!=null) destination.close();
- }
- }else{
- out.println("文件类型 error");
- }
- %>
- <img src="<%=destfile%>">
- 在 resin.conf 的web-app里 加上<multipart-form upload-max='1024kb'/>