|
发表于 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()
{
}
} |
|