不冲突啊。文件无非就是一个大字段,也不过是个字段而已。可以随FORM一起提交的
解决方案 »
- 传智播客 张孝祥老师6年后首作,JAVA邮件开发视频教程
- 高手求救 SSH框架错误!
- Axis下WebService,call.invoke()方法总是返回空值,提供的服务也没调用
- Struts标签取值出问题
- 头痛的hibernate问题-大家来看一下!!!
- struts2.0 为什么自动加入dojo.require("dojo.widget.html.Tooltip")
- 能解释一下ServerSocketConnection 的用法吗?
- 初学者的初级问题,第一个JAVA程序居然无法运行?
- 在jboss中怎样指定dataSource的jndi名字阿
- 哪里能下Netscape LDAP Server?急用!谢!
- struts+spring+jdbc碰到了一个问题,希望大家帮忙解决,我用的是spring提供的jdbctemplate类来访问数据库,有没有人知道这个模板类里面的哪个方法可以判断一个表是否存在和某个字段是走存在某个表中的方法?
- 关于请求提交的问题(struts1.3)
前端页面的form需要如下设置
<form name="newsadd_form" action="NewsInsert.jsp" method="post" enctype="multipart/form-data" onSubmit="return checkForm();">
</form>
后台代码如下:需要下载commons-fileupload.jar这个包,放在WEB-INF/lib目录下
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@page import="org.apache.commons.fileupload.*,java.io.File,java.io.IOException"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="com.linuxok.com.general.UUIDHexGenerator"%>
<%@page import="com.filemanage.interfaces.INewsFilesDAO"%>
<%@page import="com.filemanage.factory.NewsFilesFactory"%>
<%@page import="com.linuxok.com.general.FormatDate"%>
<%@page import="com.filemanage.dto.News"%>
<%@page import="com.filemanage.factory.NewsFactory"%>
<%@page import="com.filemanage.dto.Channel"%>
<%@page import="com.filemanage.factory.ChannelFactory"%>
<% request.setCharacterEncoding("GBK");
String channelId = "";
String title = "";
String news_type = "";
String pic_file = "";
String author = "";
String content_summ = "";
String content = "";
String ispinglun = "";
String option = "";String picPath = getServletContext().getRealPath("/")+"UserFiles/Image/";
String filePath = getServletContext().getRealPath("/")+"UserFiles/File/";
String tmpDirectory = getServletContext().getRealPath("/")+"tmp/"; // 申明临时目录
int maxPostSize = 4 * 1024 * 1024; // 申明限制上传文件总大小为, 单位为 byte, -1 表示无限制
DecimalFormat df = new DecimalFormat("00");
int m = 0;
String id = UUIDHexGenerator.getUUIDHex();
INewsFilesDAO newsFile = NewsFilesFactory.newInstance(); try{
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(maxPostSize); //设置文件大小. fu.setSizeThreshold(4096); //设置缓冲大小. fu.setRepositoryPath(tmpDirectory); //设置临时目录.
List fileItems = fu.parseRequest(request); //解析请求,返回一个集合.
Iterator i = fileItems.iterator(); while(i.hasNext()) {
FileItem fi = (FileItem)i.next();
//这是用来确定是否为文件属性,
if(fi.isFormField()) { // String fieldName = fi.getFieldName(); //这里取得表单名
if (fi.getFieldName().equals("channelId")) channelId = fi.getString("gbk");
if (fi.getFieldName().equals("title")) title = fi.getString("gbk");
if (fi.getFieldName().equals("news_type")) news_type = fi.getString("gbk");
if (fi.getFieldName().equals("author")) author = fi.getString("gbk");
if (fi.getFieldName().equals("content_summ")) content_summ = fi.getString("gbk");
if (fi.getFieldName().equals("content")) content = fi.getString("gbk");
if (fi.getFieldName().equals("ispinglun")) ispinglun = fi.getString("gbk");
} else { //这里开始外理文件
String fileName = fi.getName(); // 返回文件名包括客户机路径
long size = fi.getSize();
if(fileName!=null && !fileName.equals("")) {
fileName = fileName.replace('\\','/');
String ext = fileName.substring(fileName.lastIndexOf("."),fileName.length());
if (fi.getFieldName().equals("pic_file")){
pic_file = FormatDate.getDate("yyyyMMddHHmmss") + df.format(m)+ ext;
fi.write(new File(picPath+pic_file)); // 写文件到服务器.
}
//附件处理
if(fi.getFieldName().equals("option")){
option = FormatDate.getDate("yyyyMMddHHmmss") + df.format(m++) + ext;
if(!ext.equals(".exe")){
if(newsFile.insert(id,fileName,option) ){
fi.write(new File(filePath+option)); // 写文件到服务器.
}
}
}
}
}
}
}
catch(Exception e){
%>
<script language="JavaScript">
alert("文件上传错误.");
history.back();
</script>
<% return;
}
Channel channel = ChannelFactory.newInstance().getRecord(channelId);
News news = new News();
news.setId(id);
news.setChannel_id(channelId);
news.setTitle(title);
news.setNews_type(news_type);
news.setAuthor(author);
news.setIssue_state(channel.getFlag()); // 0:未审核, 1:已审核
news.setContent_summ(content_summ);
news.setContent(content);
news.setIspinglun(ispinglun);
news.setPic_file(pic_file);
if(NewsFactory.newInstance().insert(news)){
response.sendRedirect("NewsList.jsp?channelId="+channelId);
} else{
%>
<script language="JavaScript">
alert("信息提交失败,请重新提交");
history.back();
</script>
<%}%>
if (fi.getFieldName().equals("channelId")) channelId = fi.getString("gbk");
if (fi.getFieldName().equals("title")) title = fi.getString("gbk");
if (fi.getFieldName().equals("news_type")) news_type = fi.getString("gbk");
if (fi.getFieldName().equals("author")) author = fi.getString("gbk");
if (fi.getFieldName().equals("content_summ")) content_summ = fi.getString("gbk");
if (fi.getFieldName().equals("content")) content = fi.getString("gbk");
if (fi.getFieldName().equals("ispinglun")) ispinglun = fi.getString("gbk");
楼上这位仁兄的做法我是知道了,可是我是想有没更好的做法呢???
希望高手能提供点意见???
是啊,既然做了就要做到最好!不是么?jspupload这两天刚在研究