jspsmart的上传和下载

Super_qy 2009-04-27 01:51:55
表单

<form method="POST" ENCTYPE="multipart/form-data" id="upload" name="upload" action="do_upload.jsp">

<input type="file" name="file1" id="file1" />
<input type="Submit" name="Submit2" value="上传批量文件" id="label10" />
</form>
do_upload.jsp操作页面,为什么上传不了呢
<% SmartUpload su=new SmartUpload();
su.initialize(pageContext);
su.setMaxFileSize(10000);
su.setTotalMaxFileSize(20000);
su.setAllowedFilesList("mp3,mp4");// 设定允许上传的文件(通过扩展名限制),仅允许mp3,mp4文件
su.setDeniedFilesList("exe,bat,jsp,htm,html,,");//不允许exe bat jsp htm html
su.upload();
int count= su.save("e:\\upload");
out.println(count+"个文件上传成功!<br>");
%>
...全文
98 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
victorxiang 2009-04-28
  • 打赏
  • 举报
回复
强烈建议LZ用 apache的 comm-fileupload 上传

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/*
* //暂时考虑单个文件上传的处理
*/
public class FileUploadUtil{

//存放文件路径
String saveFilePath;
//存放文件名
String saveFileName;
//客户端文件路径
String clientFilePath;
//客户端文件名
String clientFileName;
//临时目录
String tempDir = "c:\temp";
long fileSize ;
public long getFileSize() {
return fileSize;
}
public String getTempDir() {
return tempDir;
}
public void setTempDir(String tempDir) {
this.tempDir = tempDir;
}
public String getSaveFilePath() {
return saveFilePath;
}
public void setSaveFilePath(String saveFilePath) {
File f = new File(saveFilePath);
if(!f.exists()) f.mkdir();
this.saveFilePath = saveFilePath;
}
public String getSaveFileName() {
return saveFileName;
}
public void setSaveFileName(String saveFileName) {
this.saveFileName = saveFileName;
}
public String getClientFilePath() {
return clientFilePath;
}
public void setClientFilePath(String clientFilePath) {
this.clientFilePath = clientFilePath;
}
public String getClientFileName() {
return clientFileName;
}
public void setClientFileName(String clientFileName) {
this.clientFileName = clientFileName;
}


public void saveFile(HttpServletRequest request) throws Exception{
String temp = "";
int p ;
try{
boolean isMultipart = FileUpload.isMultipartContent(request);
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
//文件最大,设为-1表示不受限制
upload.setSizeMax(-1);
//解析请求,把解析的结果放在一个List里面
List items = upload.parseRequest(request);
//缓存大小,设为-1表示不受限制
factory.setSizeThreshold(-1);
//设置临时目录
File ff = new File(tempDir);
if(!ff.exists()) ff.mkdir();
factory.setRepository(new File(tempDir));
Iterator iter = items.iterator();
while( iter.hasNext() ){
FileItem item = (FileItem)iter.next();
//检查是一个普通的表单域还是File组件
if( !item.isFormField() ){
System.out.println("FileName:==>"+item.getName());
System.out.println("FieldName:==>"+item.getFieldName());
System.out.println("Size:==>"+item.getSize());

temp = item.getName();
p = temp.lastIndexOf("/");
if(p == -1){
p = temp.lastIndexOf("\\");
}
if( p > 0 ){
this.clientFileName = temp.substring(p);
this.clientFilePath = temp.substring(0,p);
}
this.fileSize = item.getSize();


//item.getName 返回的是完整的文件名,如:E:\\xx\11.doc
//所以这里用一个fullFile来取文件名
// File fullFile = new File(item.getName());
if(saveFilePath.endsWith("/")) temp = this.saveFilePath+this.saveFileName;
else temp = this.saveFilePath+"/"+this.saveFileName;
File uploadedFile = new File(temp);
item.write(uploadedFile);
}
}
}catch(Exception e){
e.printStackTrace();
throw new Exception("处理上传文件失败"+e.getMessage());
}
}

}
Study_Work_2009 2009-04-28
  • 打赏
  • 举报
回复
页面有导入上传文件的包吗
Super_qy 2009-04-27
  • 打赏
  • 举报
回复
谢谢,zhuqingyang1985@126.com
witlili 2009-04-27
  • 打赏
  • 举报
回复
我有一个jspsmartupload 写的上传功能的例子,使用了数据库,把文件名和描述 还有路径写入的例子。我测试过,很好用。
你把jar包不要忘了添加到lib文件夹下。
如果需要,把邮箱告诉我。
ke0825 2009-04-27
  • 打赏
  • 举报
回复
我上传文件就是用的这个
我写了一个文件上传类
ke0825 2009-04-27
  • 打赏
  • 举报
回复
用import org.apache.struts.upload.FormFile;
这个struts自带的上传组件吧
、速度快
Super_qy 2009-04-27
  • 打赏
  • 举报
回复
没人知道吗?
Super_qy 2009-04-27
  • 打赏
  • 举报
回复
有做过的,帮忙看一下,是怎么个用法啊,谢谢了
Super_qy 2009-04-27
  • 打赏
  • 举报
回复
....................
x114944880 2009-04-27
  • 打赏
  • 举报
回复
没有使用过!
Super_qy 2009-04-27
  • 打赏
  • 举报
回复
谢谢了
Super_qy 2009-04-27
  • 打赏
  • 举报
回复
第一次用这个组件,不会啊,帮忙看下我哪错了。。。。。。。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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