最近要实现一个上传技术,起初考虑使用struts1.2的html标签来实现上传功能,但是后来发现如果使用html标签那么在上传页面的布局,则不是那么灵活了! 后来放弃了这种思路,寻找使用传统标签来实现上传功能!
1。定义上传页面;
<form name="form1" action="/ss/upFileAction.do" target="" method="post" enctype="multipart/form-data">
<table class="table_info2" cellpadding="3" border="0.3" cellspacing="1">
<tr>
</tr>
<tr>
<td><input type="file" name="theFile" id="theFile" ></td>
</tr>
<tr>
<td><input type="text" id="descFile" value="" name="descFile">(文件描述)</td>
</tr>
<tr>
<td> <input type="checkbox" id="replace" name="replace" value="1">是否覆盖同名附件</td>
</tr>
<tr>
<td><input type="button" id="button" value="提交" onClick="upfile();"> <input type="button" value="取消" onClick="closeOpen();"></td>
</tr>
</table>
</form>
2。定义要使用的actionform,在其中使用file类型的字段,处理上传流!
ActionForm:
protected FormFile theFile;
3。后台处理传来的文件流,并保存到指定的服务器目录下;
public ActionForward upfile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionForward forward = null;
if (form instanceof UpFileForm) {// 如果form是UpLoadsForm
response.setContentType("text/html; charset=GBK");
UpFileForm theForm = (UpFileForm) form;
String replace = theForm.getReplace();
System.out.println("-=-=>"+theForm.getDescFile());
FormFile file = theForm.getTheFile();// 取得上传的文件
String contentType = file.getContentType();
String size = (file.getFileSize() + " bytes");// 文件大小
String fileName = file.getFileName();// 文件名
String area_code = request.getParameter("h_xian");
String yljg = request.getParameter("yljg");
String serverPath = request.getParameter("serverPath");
String desc = theForm.getDescFile();
try {
InputStream stream = file.getInputStream();// 把文件读入
//创建母文件夹
String folder_path = serverPath+"\\"+area_code+"\\"+yljg;
File folder_file =new File(folder_path);
if(!folder_file.exists()){
folder_file.mkdirs();
}
if(replace==null||"".equals(replace)){
if(getUpFileName(folder_path,fileName)){
request.setAttribute("S_ERROR_MSG", "对不起,上传的文件名重复,请重新命名!");
return mapping.findForward("error");
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(folder_path + "\\"
+ fileName);
// 建立一个上传文件的输出流,将上传文件存入web应用的根目录。
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件写入服务器
}
bos.close();
stream.close();
} catch (Exception e) {
System.err.print(e);
e.printStackTrace();
request.setAttribute("S_ERROR_MSG", "对不起,附件上传失败!");
return mapping.findForward("error");
}
return mapping.findForward("input");
}
return null;
}
上传实现后,如果要实现下载功能,则思路是这样的:
只要获取该文件所在的http路径,然后写在a标签的href属性中,然后设定target属性值为a便可! 当我们点击该链接是,必可进入都该文件可! 我们可以对该链接右键进行目标另存为或者迅雷下载! 如果我们要下载的是doc文件或者excel文件,在点击链接后,便可弹出保存或者打开对话框! 也就是说访问web上的资源,是这样的情况! 如果路径为本地路径,访问本地资源,点击链接,则直接弹出保存对话框! 这是web访问和本地访问的不同方式!
分享到:
相关推荐
我觉得struts1.2是一个比较稳定的框架,就连网上有名的教授都推荐用struts1.2,所以没有驱动的友友赶紧下载吧!
如题,本项目是一个简单的解决关于Struts1.2上传下载问题的Demo。 希望对需要的你有所帮助。
struts1.2 jar包 struts1.2 jar包 struts1.2 jar包 struts1.2 jar包 struts1.2 jar包
struts1.2 简单文件上传例子(用eclipse3.4打开运行即可)
这是一个struts 1.2 + spring 2.5 + hibernate 3.2框架demo, 运行环境为eclipse 3.2 + tomcat 5.5 + oracle 11g
struts 1.2驱动包struts 1.2驱动包
struts1.2源码.rar 核心源码 struts-1.2.4-lib Struts1.2API chm格式
struts1.2-2.0学习总结
NULL 博文链接:https://lzj0470.iteye.com/blog/1941682
struts1.2标签实例超经典,方便大家学习,对初学者有很好帮助
struts1.2实现图片上传,先存放在服务器tomcat webappt里面。。。。
STRUTS1.2中文文档.rar,里边包含很多的STRUTS1.2相关文档,认真的看看,你会有收获的
struts1.2实现动态多文件上传,可实现代码全奉献!
struts 1.2 标签使用说明,简单介绍了struts1.2的标签使用
用于struts1.2学习资料,用于查询时用的
jsp中 struts1.2 包 asdasdasd dasd a
struts1.2项目指导struts spring hibernate
struts1.2拦截器使用插件
struts1.2 商城购物 随意写的
struts1.2框架技术的基本原理及其组件的用法