Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 708|回复: 0

apache-commons-fileupload问题

[复制链接]

该用户从未签到

发表于 2011-11-4 10:16:15 | 显示全部楼层 |阅读模式
Java学習者论坛
哪位大哥用过commons-fileupload?
我现在能上传文件了,但无法得到表單的参数,
用String man_name=request.getParameter("man_name");
没有效果!


欢迎來到Java学習者論坛,转载请注明地址:http://www.javaxxz.com.
回复

使用道具 举报

该用户从未签到

发表于 2011-11-4 10:16:50 | 显示全部楼层
谢谢,问题已解决,我的代碼:
package com.sxztb.upload;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import com.sxztb.db.*;
public class uploadServlet extends HttpServlet
{
   private static final String CONTENT_TYPE = "text/html; charset=GBK";
   //Initialize global variables
   public void init() throws ServletException
   {
   }
   //Process the HTTP Get request
   public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
   {
       response.setContentType(CONTENT_TYPE);
       PrintWriter out = response.getWriter();
       out.println("<html>");
       out.println("<head><title>uploadServlet</title></head>");
       out.println("<body>");
       out.println("<p>The servlet has received a GET. This is the reply.</p>");
       out.println("</body></html>");
   }
   /*
   String Mkdir(String path)
   {
       String msg=null;
       java.io.File dir;
       dir =new java.io.File(path);
       if (dir == null)
       {
           msg = "错误原因:<BR>对不起,不能创建空目录!";
           return msg;
       }
       if (dir.isFile())
       {
           msg = "错误原因:<BR>已有同名文件<B>" + dir.getAbsolutePath() + "</B>存在。";
           return msg;
       }
       if (!dir.exists())
       {
           boolean result = dir.mkdirs();
           if (result == false)
           {
               msg = "错誤原因:<BR>目录<b>" + dir.getAbsolutePath() + "</B>创建失败,原因不明!";
               return msg;
           }
           // 如果成功创建目录,则无輸出。
           // msg ="成功创建目录: <B>" + dir.getAbsolutePath() + "</B>";
           return msg;
       }
       else
       {
           //msg = "錯误原因:<BR>目錄<b>" + dir.getAbsolutePath() + "</b>已存在。";
       }
       return msg;
   }
   */
   String getCurDate()
   {
       java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy年MM月dd日");
       java.util.Date currentTime_1 = new java.util.Date();
       String ss=formatter.format(currentTime_1);
       return ss;
   }
   //Process the HTTP Post request
   public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
   {
       try
       {
           response.setContentType(CONTENT_TYPE);
           PrintWriter out = response.getWriter();
           String server_path=request.getRealPath("/");
           String saveDirectory =server_path+"/file_pic/";
           String tmpDirectory = "c:
";
           int maxPostSize = 1024 * 1024;
           boolean is_legal_file=false;
           String FileDescription = null;
           String FileName = null;
           long FileSize = 0;
           String ContentType = null;
           int count = 0 ;
           String sql="";
           java.util.Random random = new java.util.Random();
           int i=random.nextInt();
           i=Math.abs(i);
           String random_no=""+i;
           java.util.Vector text = new java.util.Vector();//存放非文件域信息
           java.util.Vector vector_file = new java.util.Vector();//存放文件域信息
           DiskFileUpload upload = new DiskFileUpload();//初始化上传组件
           upload.setSizeThreshold(4096);//设置文件流大小
           upload.setSizeMax(maxPostSize);//设置最大文件,超过则临时存放到:tmpDirectory
           upload.setRepositoryPath(tmpDirectory);//设置临时存放點
           List  items = upload.parseRequest(request);//取得request
           Iterator iter = items.iterator();//初始化
           int file_no=0;
           int tmp = 0;
           FileItem tmpItem = null;
           while (iter.hasNext())//表单有多个域
           {
               tmp++;
               FileItem item = (FileItem) iter.next();
               if (item.isFormField())//如果是非文件域信息
               {
                   FileDescription = item.getString();//取得非文件域信息,皆为字符串类型
                   text.addElement(FileDescription);//加入到存放非文件域信息之向量
               }
               else //如果是文件域信息
               {
                   file_no++;
                   FileName = item.getName();
                   try //因为不同的浏览器会造成 path + filename, 有些則只有 filename
                   {
                       // for wintel platform
                       FileName = FileName.substring(FileName.lastIndexOf("
")+1);//取得档案名稱
                       // for unix-like platform
                       FileName = FileName.substring(FileName.lastIndexOf("/")+1);//取得档案名稱
                   }
                   catch (Exception ex)
                   {
                       System.out.println("取文件名出错:"+ex);
                   }
                   ContentType = item.getContentType();//取得档案类型
                   FileSize = item.getSize();//取得档案大小
                   tmpItem = item;
                   try // 將檔案寫入存檔目錄
                   {
                       //转换文件名
                       if(FileSize!=0)
                       {
                           java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
                           java.util.Date currentTime_1 = new java.util.Date();
                           String ss=formatter.format(currentTime_1);
                           String FileName_houzhui=FileName.substring(FileName.lastIndexOf("."));
                           FileName=ss+"-"+file_no+"-"+random_no+FileName_houzhui;
                           vector_file.addElement(FileName);
                           File uploadedFile = new File(saveDirectory + FileName);
                           tmpItem.write(uploadedFile);
                       }
                   }
                   catch(Exception ex)
                   {
                       System.out.println("保存文件出错:"+ex);
                       System.out.println("错误位置:/servlet/com.sxztb.upload.uploadServlet");
                   }
               }
           }
           //转到写文件類,完成写文件
           try
           {
               String file1=null;
               String file2=null;
               String file3=null;
           upload_write_file_servlet tody_log = new upload_write_file_servlet(server_path);
               if(vector_file.size()!=0)
               {
                   file1=(String)vector_file.elementAt(0);
               }
               if(vector_file.size()>1)
               {
                    file2=(String)vector_file.elementAt(1);
               }
               if(vector_file.size()>2)
               {
                    file3=(String)vector_file.elementAt(2);
               }
               sql=tody_log.log_wite((String)text.elementAt(0),(String)text.elementAt(1),(String)text.elementAt(2),(String)text.elementAt(3),file1,file2,file3);
               System.out.println("在uploadServlet中"+sql);
               //写日期
               String contextPath = request.getRealPath("/");
               log now_log = new log(contextPath);
               HttpSession session =request.getSession();
               now_log.log_wite(sql,request.getRemoteAddr(),request.getRemoteHost(),(String)session.getAttribute("user_name"));
           }
           catch (Exception e)
           {
               System.out.println("生成JSP文件出现错误!"+e);
               System.out.println("错误位置:/servlet/com.sxztb.upload.uploadServlet");
           }
           //写文件结束,轉到相应目录
           getServletContext().getRequestDispatcher("/test/upload/").forward(request,response);
       }
       catch(Exception e)
       {
           System.out.println("/servlet/com.sxztb.upload.uploadServlet出错"+e);
       }
   }
   //Clean up resources
   public void destroy()
   {
   }
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-3 07:56 , Processed in 0.385914 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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