注册
 找回密码
 注册
江西广告网
查看: 287|回复: 0
打印 上一主题 下一主题

commons

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2009-2-12 09:00:28 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>upload</servlet-name> <servlet-class>com.Upload</servlet-class> </servlet> <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>/upload.do</url-pattern> </servlet-mapping> </web-app> index.jsp page: <RE class=html name="code"><%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>file upload demo</title> </head> <body> <center> <h1>file upload demo</h1> <form name="uploadform" method="post" action="<%=request.getContextPath()%>/upload.do" enctype="multipart/form-data"> <table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9bd7ff"> <tr> <td width="100%" colspan="2">dir:<input name="dir" size="40" type="text"></td> </tr> <tr> <tr> <td width="100%" colspan="2">file1:<input name="file1" size="40" type="file"></td> </tr> <tr> <td width="100%" colspan="2">file2:<input name="file2" size="40" type="file"></td> </tr> <tr> <td width="100%" colspan="2">file3:<input name="file3" size="40" type="file"></td> </tr> <tr> [1] [2] [3] <td align="center"><input name="upload" type="submit" value="upload"> </td> </tr> </table> </form> </center> </body> </html> </PRE> Upload.java : <RE class=java name="code">package com; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.Iterator; 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; public class Upload extends HttpServlet { private static final long serialVersionUID = 1976222622672301465L; @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { DiskFileItemFactory factory = new DiskFileItemFactory(); File tempDir = new File("D:\\temp"); if (!tempDir.exists()) { tempDir.mkdirs(); } factory.setRepository(tempDir); factory.setSizeThreshold(10 * 1024); ServletFileUpload fu = new ServletFileUpload(factory); fu.setSizeMax(1000000000); fu.setHeaderEncoding("UTF-8"); // maximum size that will be stored in memory? 上一页 [1] [2] [3] try { List items = fu.parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fileName = item.getName(); if (fileName != null && !fileName.trim().equals("")) { if (item.isFormField()) { String name = item.getFieldName(); String value = new String(item.getString() .getBytes("UTF-8"), "UTF-8"); System.out.println("name: " name); System.out.println("value: " value); } else { // save to disk String imageDir = "D:\\target\\"; System.out.println(request.getCharacterEncoding()); File saveDir = new File(imageDir); if (!saveDir.exists()) { saveDir.mkdirs(); } if (fileName != null && !fileName.trim().equals("")) { int i = fileName.lastIndexOf("\\"); fileNamefileName = fileName.substring(i); File saveFile = new File(saveDir, fileName); if (!saveFile.exists()) { saveFile.createNewFile(); } item.write(saveFile); } // save to DB Connection conn = null;// if conn already received. PreparedStatement pstmt = conn .prepareStatement("insert into test values(?)"); InputStream is = item.getInputStream(); pstmt.setBinaryStream(1, is, (int) item.getSize()); pstmt.executeUpdate(); is.close(); pstmt.close(); conn.close(); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }</PRE> 上一页 [1] [2] [3]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表