这是上传文件的页面<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>File upload</title>
</head>
<body>
<form name="myform" action="doupload.html" method="post" enctype="multipart/form-data">
<p><input type="file" name="myfile"></p>
<p><input type="submit" name="submit" value="Commit"></p>
</form>
</body>
</html>已将Upload.java映射为doupload.html
package textarea;import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
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.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;public class Upload extends HttpServlet { private String uploadPath = "/hello/upload/"; // 上传文件的目录 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
PrintWriter out = response.getWriter();
try {
File tempfile = new File("/hello/upload/"); //采用系统临时文件目录
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
diskFileItemFactory.setRepository(tempfile); //设置缓冲区目录
ServletFileUpload fu = new ServletFileUpload(diskFileItemFactory);
fu.setSizeMax(4194304);
try{
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
out.println(i.hasNext());
while (i.hasNext()) {
FileItem item = (FileItem) i.next();
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
}else{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println(fieldName);
out.println(fileName);
out.println(contentType);
out.println(isInMemory);
out.println(sizeInBytes);
}
}
}
catch(FileUploadException e){
e.printStackTrace();
}
/*if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
else
out.println("Nothing");
//out.println("<p>" + fileName + "</p>");*/
}catch(Exception e) {
e.printStackTrace();
}
} public void init() throws ServletException {
File uploadPath = new File("/hello/upload/");
if (!uploadPath.exists()) {
uploadPath.mkdirs();
} }
}请问是哪里出错了, 提交表单后, 总上传不了, 上传目录是空的!!
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>File upload</title>
</head>
<body>
<form name="myform" action="doupload.html" method="post" enctype="multipart/form-data">
<p><input type="file" name="myfile"></p>
<p><input type="submit" name="submit" value="Commit"></p>
</form>
</body>
</html>已将Upload.java映射为doupload.html
package textarea;import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
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.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;public class Upload extends HttpServlet { private String uploadPath = "/hello/upload/"; // 上传文件的目录 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
PrintWriter out = response.getWriter();
try {
File tempfile = new File("/hello/upload/"); //采用系统临时文件目录
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
diskFileItemFactory.setRepository(tempfile); //设置缓冲区目录
ServletFileUpload fu = new ServletFileUpload(diskFileItemFactory);
fu.setSizeMax(4194304);
try{
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
out.println(i.hasNext());
while (i.hasNext()) {
FileItem item = (FileItem) i.next();
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
}else{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println(fieldName);
out.println(fileName);
out.println(contentType);
out.println(isInMemory);
out.println(sizeInBytes);
}
}
}
catch(FileUploadException e){
e.printStackTrace();
}
/*if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
else
out.println("Nothing");
//out.println("<p>" + fileName + "</p>");*/
}catch(Exception e) {
e.printStackTrace();
}
} public void init() throws ServletException {
File uploadPath = new File("/hello/upload/");
if (!uploadPath.exists()) {
uploadPath.mkdirs();
} }
}请问是哪里出错了, 提交表单后, 总上传不了, 上传目录是空的!!
解决方案 »
- System.getProperty("file.encoding") 不明白
- 一个存值和取值的问题。。。高手给看下~
- 如何刷新父窗口,我的弹出窗口使用的showModalDialog?
- 连接数据库奇怪的问题
- JSP操作SQL SERVER 2000游标错误的问题
- HTTP GET请求中所涉及到的网络协议
- rs.getString()与oracle里的clob字段问题?!急救!!!在线等!
- 让我们一起成为高手!
- 安装了jdk1.3后为什么编译一个servlet出错!是不是还要安装什么东西呢?
- 简单问题,请你一答!
- servlet 访问8次后就不能再访问了??????
- struts中的actionform为空
谢谢您, 我调试过, List fileItems = fu.parseRequest(request); 这个fileItems为空的, 路径我换过绝对路径("c:\\upload\\"), 但还是上传不了文件, 上传目录始终为空.回:canghaiguzhou
谢谢您, 我已经在classpath中添加了commons-io-1.4.jar了, 编译时也没出错, 就是获取不到上传的文件.咋办了?
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import java.util.*;public class UploadServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
//设置保存上传文件的目录
String uploadDir = getServletContext().getRealPath("/upload");
if (uploadDir == null)
{
out.println("无法访问存储目录!");
return;
}
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists())
{
if(!fUploadDir.mkdir())
{
out.println("无法创建存储目录!");
return;
}
}
if (!DiskFileUpload.isMultipartContent(request))
{
out.println("只能处理multipart/form-data类型的数据!");
return ;
}
DiskFileUpload fu = new DiskFileUpload();
//最多上传200M数据
fu.setSizeMax(1024 * 1024 * 200);
//超过1M的字段数据采用临时文件缓存
fu.setSizeThreshold(1024 * 1024);
//采用默认的临时文件存储位置
//fu.setRepositoryPath(...);
//设置上传的普通字段的名称和文件字段的文件名所采用的字符集编码
fu.setHeaderEncoding("gb2312");
//得到所有表单字段对象的集合
List fileItems = null;
try
{
fileItems = fu.parseRequest(request);
}
catch (FileUploadException e)
{
out.println("解析数据时出现如下问题:");
e.printStackTrace(out);
return;
}
//处理每个表单字段
Iterator i = fileItems.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
if (fi.isFormField())
{
String content = fi.getString("GB2312");
String fieldName = fi.getFieldName();
request.setAttribute(fieldName,content);
}
else
{
try
{
String pathSrc = fi.getName();
/*如果用户没有在FORM表单的文件字段中选择任何文件,
那么忽略对该字段项的处理*/
if(pathSrc.trim().equals(""))
{
continue;
}
int start = pathSrc.lastIndexOf('\\');
String fileName = pathSrc.substring(start + 1);
File pathDest = new File(uploadDir, fileName); fi.write(pathDest);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName, fileName);
}
catch (Exception e)
{
out.println("存储文件时出现如下问题:");
e.printStackTrace(out);
return;
}
finally //总是立即删除保存表单字段内容的临时文件
{
fi.delete();
}
}
}
//显示处理结果
out.println("用户:" + request.getAttribute("author") + "<br>");
out.println("来自:" + request.getAttribute("company") + "<br>");
/*将上传的文件名组合成“file1,file2”这种形式显示出来,如果没有上传
*任何文件,则显示“无”,如果只上传了第二个文件,显示为“file2”。*/
StringBuffer filelist = new StringBuffer();
String file1 = (String)request.getAttribute("file1");
makeUpList(filelist,file1);
String file2 = (String)request.getAttribute("file2");
makeUpList(filelist,file2);
out.println("成功上传的文件:" +
(filelist.length()==0 ? "无" : filelist.toString()));
}
/**
*将一段字符串追加到一个结果字符串中。如果结果字符串的初始内容不为空,
*在追加当前这段字符串之前先最加一个逗号(,)。在组合sql语句的查询条件时,
*经常要用到类似的方法,第一条件前没有"and",而后面的条件前都需要用"and"
*作连词,如果没有选择第一个条件,第二个条件就变成第一个,依此类推。
*
*@param result 要将当前字符串追加进去的结果字符串
*@param fragment 当前要追加的字符串
*/
private void makeUpList(StringBuffer result,String fragment)
{
if(fragment != null)
{
if(result.length() != 0)
{
result.append(",");
}
result.append(fragment);
}
}
}