第2次提一样的问题

yangxuebao123 2009-03-22 03:03:56
请问在web开发中,上传文件怎么做。
...全文
139 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Study_Work_2009 2009-03-25
  • 打赏
  • 举报
回复
up
Ghost_520 2009-03-25
  • 打赏
  • 举报
回复

既然你用到了 SSH ,那就用 commons-fileupload 上传吧,用 smartupload 也可以,前提条件是在工程目录下存在
commons-fileupload.jar,可能还会需要 commons-beanutils.jar


commons-fileupload 上传代码:

<%@ page language="java"
import="java.util.*,java.io.*,org.apache.commons.fileupload.*"
pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uploadJSPBack.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />

<%
response.setCharacterEncoding("GBK");
String uploadPath = "";
String tempPath = "d:\\";
try {
System.out.println("开始进行文件上传");
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
fu.setRepositoryPath(tempPath); // 设置临时目录
List fileItems = fu.parseRequest(request); // 得到所有的文件:
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();// 获得文件名,这个文件名包括路径:
if (fileName != null) {
// 在这里可以记录用户和文件信息
// 此处可以定义一个接口(CallBack),用于处理后事。
// 写入文件a.txt,你也可以从fileName中提取文件名:
String name = fileName.substring(0, fileName
.indexOf("."));
String extfile = fileName.substring(fileName
.indexOf("."));

//上传时间作为文件名,用以防止重复上传
//Timestamp now = new Timestamp((new java.util.Date()).getTime());
//SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//String pfileName= fmt.format(now).toString().trim();
System.out.println(name + extfile);
fi.write(new File(name + extfile));
}
}

System.out.println("上传成功");

response.setContentType("text/html;charset=utf-8");
response.getWriter().print("{success:true,message:'上传成功'}");
// 跳转到上传成功提示页面
} catch (Exception e) {
e.printStackTrace();
response.getWriter().print("{success:false,message:'上传失败'}");
// 可以跳转出错页面
}
%>
</head>

<body>
</body>
</html>



smartupload.jar 上传代码: 当然前提条件是在你的工程下存在 jspsmartupload.jar

<%@ page contentType="text/html; charset=GBK" %>

<html>
<head>
<title>
uploadPic
</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: 12px;
color: #0066FF;
}
-->
</style>
<script type="">
function check(){
var filePath1 = document.form1.file.value;
var filePath2 = document.form1.file2.value;
var filePath3 = document.form1.file3.value;

if((filePath1 == "") && (filePath2 == "") && (filePath3 == "")){
alert("您还没有选择上传的图片呢!");
return false;
}
else{
return true;
}
}
</script>
</head>
<body bgcolor="#ffffff">
<h1 align="center">广告信息的图片上传</h1>
<form action="Upload.jsp" method="post" enctype="multipart/form-data" name="form1" onsubmit="return check()">
<table width="420" height="169" align="center" cellpadding="0" border="1" cellspacing="0" style="border-bottom:1px solid">
<caption>选择多张图片后点击上传即可</caption>
<tr>
<td width="74"><div align="center"><span class="STYLE1">图片1</span>:</div></td>
<td width="340"><input name="file" type="file" size="30"></td>
</tr>
<tr>
<td height="31"><div align="center" class="STYLE1">图片2:</div></td>
<td><input name="file2" type="file" size="30"></td>
</tr>
<tr>
<td><div align="center" class="STYLE1">图片3:</div></td>
<td><input name="file3" type="file" size="30"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="上传">
<input type="reset" name="Submit2" value="取消"></td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>



<%@page contentType="text/html; charset=GBK"%>
<%@page import="com.jspsmart.upload.*"%>
<html>
<head>
<title>Upload</title>
</head>
<body bgcolor="#ffffff">
<%
/* 用此组件上传文件,若存在相同名称的文件,它会自动选择覆盖 */

try {
request.setCharacterEncoding("GBK");

SmartUpload su = new SmartUpload();

int countUpload = su.getFiles().getCount();

//初始化页面作用域
su.initialize(pageContext);

su.service(request, response);

//限制上传文件的类型,允许格式为 jpg,gif,bmp
su.setAllowedFilesList("jpg,gif,bmp");

//禁止文件上传类型
su.setDeniedFilesList("exe,bat,jsp,htm,html,");

su.upload();

//记录成功上传图片的数目
int count =0;
/*
* 如果想上传的图片以自己定义的名称保存的话,使用以下的方法
*
* 注意:使用这种方式保存的话,下面的 su.save("/image"); 此方法就不需要了,否则就重复保存了

int size = su.getFiles().getCount();
for(int i=0;i<size;i++){
File file = su.getFiles().getFile(i);
file.saveAs("/image/"+(i+1)+".jpg");
count++;
}*/

//将这些图片保存到站点下的 image 文件夹下,并得到成功上传文件数目
count = su.save("/image");

out.println("<script>");
out.println("alert('成功上传图片 "+count+" 张!');history.back(1);");
out.println("</script>");
}
catch (SmartUploadException ex) {
out.print(ex.toString());
}
catch(Exception e){
out.println("<script>");
out.println("alert('存在不允许上传的格式,只允许上传后缀为 jpg|gif|bmp 格式的图片,请重新选择文件!');history.back(1);");
out.println("</script>");
}
%>
</body>
</html>
yangfeitarena 2009-03-25
  • 打赏
  • 举报
回复
jsp页面:
<body>
<br>
<html:form action="upload.do" method="post" enctype="multipart/form-data">
<html:file property="imgFile"> </html:file> <br>
<html:text property="imgName" value=""> </html:text> <br>
<html:submit value="上传"> </html:submit>
</html:form>
</body>

struts-config.xml如下:
<form-beans>
<form-bean name="upfileFrom" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="imgName" type="java.long.String"> </form-property>
<form-property name="imgFile" type="org.apache.struts.upload.FormFile"> </form-property>
</form-bean>
</form-beans>

<action-mappings>
<action
path="/upload"
name="upfileFrom"
input="/index.jsp"
validate="false"
type="org.springframework.web.struts.DelegatingActionProxy"
>
</action>
</action-mappings>
xtbzqw 2009-03-23
  • 打赏
  • 举报
回复
用fileupload!
DJhank 2009-03-23
  • 打赏
  • 举报
回复
恩,看楼主具体想用什么框架...
daisycool 2009-03-23
  • 打赏
  • 举报
回复
看你想用什么做。php几行代码就可以搞定,JAVA稍微麻烦点。

上传一个文件好做,想批量上传文件有点复杂,主要看LZ想做什么,用什么做。
yangxuebao123 2009-03-23
  • 打赏
  • 举报
回复
第一次问没个结果,第二次也是一样的,加分都到200了、
网上的很杂,我想用到struts+hibernate+spring 项目中。
就没有详细点的方法吗。
liang__ 2009-03-23
  • 打赏
  • 举报
回复
真的哦,网上多的很呢。
为什么楼主说第二次发贴问样的问题?
hubert_bubert 2009-03-23
  • 打赏
  • 举报
回复
cos 组件
liup19751227 2009-03-23
  • 打赏
  • 举报
回复
fileupload
Struts中也有上传组件
luoping544 2009-03-23
  • 打赏
  • 举报
回复
SmartUpload
zhuhong110450 2009-03-23
  • 打赏
  • 举报
回复
看你是用那种方式进行上传了,如果用普通的方式,需要导入SmartUpload.jar包;
如果是StrutsMVC模式就用其框架自带的上传组件,或导入fileupload.jar包.
jklot66 2009-03-23
  • 打赏
  • 举报
回复
struts有一个上传的主键<html:file>
lutao050306 2009-03-23
  • 打赏
  • 举报
回复
比较好用的一个是smartupload。自己搜索一下
APOLLO_TS 2009-03-23
  • 打赏
  • 举报
回复
http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
zhoushijin5201314 2009-03-23
  • 打赏
  • 举报
回复
我的博客有例子
qq707472 2009-03-22
  • 打赏
  • 举报
回复
我想给你代码, 但是太多,给我邮箱的话我可以传给你。 我的邮箱:x5201314x@vip.qq.com
三月暖阳 2009-03-22
  • 打赏
  • 举报
回复
上传文件有好几种,你看你用什么了,具体的话应该从网上搜索具体的,你用google搜索文件上传看看你能搜索到多少结果
lakesea 2009-03-22
  • 打赏
  • 举报
回复
这个网上应该有很多现成的组件吧,

81,078

社区成员

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

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