求助:SmartUpload上传文件问题

yunzm_wang 2010-09-16 04:58:17
jsp页面:
<%@ page contentType="text/html; charset=utf-8" language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文件上传</title>



</head>

<body>

<table border="0" cellpadding="2px" cellspacing="0px">
<tr><td>
<form name="UploadForm" enctype="multipart/form-data" method="post" action="../../UpFileServlet.do">
<input type="file" id="ff" class="T_B_L_R" />
<input type="submit" value="上传" class="T_B_L_R" />
</form>
</td></tr>
<div id="more" />
<tr>
<td valign="middle" align="center">

</td>
</tr>
</table>

</body>
</html>



servlet:dopost方法


long size=20*1024*1024;
SmartUpload sup = new SmartUpload();

try{
sup.initialize(this.getServletConfig(), request, response);
System.out.println("begin");
sup.setMaxFileSize(size); //最大允许上传20M
sup.setAllowedFilesList("txt");
sup.upload();
System.out.println("size:"+sup.getSize());
System.out.println("count:"+sup.save("/upload/normal/"));

}
catch(Exception e){
e.printStackTrace();
}


size可以得到49但是目录下找不到文件。count得到是0。sup.getFiles().getCount()得到也是0。

...全文
191 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengjing123456 2010-09-17
  • 打赏
  • 举报
回复
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.FileUploadException;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final long MAX_SIZE = 20 * 1024 * 1024;// 设置上传文件最大为 20M

final String[] allowedExt = new String[] { "doc", "docx", "xls",
"xlsx", "pptx", "ppt" };//允许的文件类型
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");

//磁盘文件存储工厂实例
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(409600); // 400k 临时内存
dfif.setRepository(new File(request.getSession().getServletContext().getRealPath("/") + "datas/temp"));//临时文件目录
// 用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
sfu.setSizeMax(MAX_SIZE);//设置文件上传的大小
PrintWriter out = response.getWriter();
// 从request得到 所有上传域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request); //得到上传文件列表的文件信息

} catch (FileUploadException e) {// 处理文件尺寸过大异常

return;
}

if (fileList == null || fileList.size() == 0) {
return;
}
FileItem fileItem = (FileItem) fileList.get(0);//此处只有一个文件,就不遍历了
String path = null;
long size = 0;
path = fileItem.getName();

size = fileItem.getSize();
if ("".equals(path) || size == 0) {
return;
}
// 得到去除路径的文件名
String t_name = path.substring(path.lastIndexOf("\\") + 1);

// 检验文件类型是否合法
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {

for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out
.println("*." + allowedExt[allowFlag]
+ "   ");

return;
}
String u_name = request.getSession().getServletContext().getRealPath("/") + "datas/doc/" + t_name;
try {
// 保存文件

fileItem.write(new File(u_name));
deletedoc("datas/temp", request);
} catch (Exception e) {
e.printStackTrace();
}
}

//删除临时文件
public void deletedoc(String dirpath,HttpServletRequest request){

String path=request.getSession().getServletContext().getRealPath(dirpath);
File f=new File(path);
String[] f1= f.list();
for (int i = 0; i < f1.length; i++) {
File f2=new File(path+"\\"+f1[i]);
f2.delete();
}
}
dr_lou 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yunzm_wang 的回复:]
问题解决了。
<input type="file" id="ff" class="T_B_L_R" />
改成
<input type="file" name="ff" class="T_B_L_R" />
就可以正常上传了。测试了几遍,如果是id就不行,name就OK。搞不明白怎么回事。

我一直都是搞CS的,最近刚开始搞BS,对这些不怎么懂。这个id和name有什么区别吗……
[/Quote]

因为request.getParameter("name") 找的是name
document.getElementById用的是id
lnner 2010-09-17
  • 打赏
  • 举报
回复
先不要设置文件大小试试
try
{
su.initialize(config, request, response);
su.upload();
}
catch (SmartUploadException e)
{
// UpdcolresService.delColRes(seqValue);
LogManager.warning(e.getMessage());
e.printStackTrace();
}
catch (SecurityException e)
{
LogManager.getLog().warning(e.getMessage());
e.printStackTrace();
this.forword(next, request, response);
return;
}

for (int i = 0; i < su.getFiles().getCount(); i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);

int ressize=file.getSize();
//判断大小如果超过最大范围,上传失败,如果在范围内,上传成功
}
yunzm_wang 2010-09-17
  • 打赏
  • 举报
回复
问题解决了。
<input type="file" id="ff" class="T_B_L_R" />
改成
<input type="file" name="ff" class="T_B_L_R" />
就可以正常上传了。测试了几遍,如果是id就不行,name就OK。搞不明白怎么回事。

我一直都是搞CS的,最近刚开始搞BS,对这些不怎么懂。这个id和name有什么区别吗?在应用的时候有什么特殊的地方吗?
生活 2010-09-17
  • 打赏
  • 举报
回复
我只做过flex往java服务器上传文件。也是用servlet没出现你的问题。
24K純帥 2010-09-17
  • 打赏
  • 举报
回复
偶也觉得是路径问题,要不有个例子看看
http://dev.csdn.net/htmls/18/18987.html
yunzm_wang 2010-09-17
  • 打赏
  • 举报
回复
下面是用FileUpLoad方法做的,代码是在网上down的。执行到红色字体部分,跟踪出来文件个数是0


final long MAX_SIZE = 3 * 1024 * 1024;// 设置上传文件最大为 3M
response.setContentType("text/html");
// 设置字符编码为UTF-8, 这样支持汉字显示
response.setCharacterEncoding("UTF-8");

// 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(4096);// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘
dfif.setRepository(new File(this.getServletContext().getRealPath("") + "/upload/temp/"));// 设置存放临时文件的目录,web根目录下的ImagesUploadTemp目录

// 用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
// 设置最大上传尺寸
sfu.setSizeMax(MAX_SIZE);

PrintWriter out = response.getWriter();
// 从request得到 所有 上传域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request);
} catch (Exception e) {
e.printStackTrace();
}
// 得到所有上传的文件
Iterator fileItr = fileList.iterator();
// 循环处理所有文件
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
// 得到当前文件
fileItem = (FileItem) fileItr.next();
// 忽略简单form字段而不是上传域的文件域(<input type="text" />等)
if (fileItem == null || fileItem.isFormField()) {
continue;
}
// 得到文件的完整路径
path = fileItem.getName();
// 得到文件的大小
size = fileItem.getSize();
// 得到去除路径的文件名
String t_name = path.substring(path.lastIndexOf("\\") + 1);
// 得到文件的扩展名(无扩展名时将得到全名)
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
long now = System.currentTimeMillis();
// 根据系统时间生成上传后保存的文件名
String prefix = String.valueOf(now);
// 保存的最终文件完整路径,保存在web根目录下的ImagesUploaded目录下
String u_name = request.getRealPath("") + "/upload/normal/"
+ prefix + "." + t_ext;
try {
// 保存文件
fileItem.write(new File(u_name));
out.println("文件上传成功. 已保存为: " + prefix + "." + t_ext
+ "   文件大小: " + size + "字节<p />");
} catch (Exception e) {
e.printStackTrace();
}

}
}
yunzm_wang 2010-09-17
  • 打赏
  • 举报
回复
没有用的,相对路径和绝对路径试过都不行。servlet中用ServletFileUpload上传也是这种情况。
yunzm_wang 2010-09-17
  • 打赏
  • 举报
回复
不是路径的问题。同样的sup.getFiles().getCount()得到也是0
yunzm_wang 2010-09-17
  • 打赏
  • 举报
回复
谢谢,各位,结贴。
gzb4562 2010-09-16
  • 打赏
  • 举报
回复
文件上传时路径可能有问题,要用相对路径
David_0328 2010-09-16
  • 打赏
  • 举报
回复
应该是存放路径的问题
生活 2010-09-16
  • 打赏
  • 举报
回复
路径写的不对吧。如果文件大小的到了 那应该能取到文件,你看看关于File的操作吧

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧