读书人

文件下传不到数据库

发布时间: 2012-09-03 09:48:39 作者: rapoo

文件上传不到数据库
jsp代码:
<body>

<s:form action="addNews" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="900" border="1">
<caption>
发布业内新闻
</caption>
<tr>
<td>标题:</td>
<td><label>
<input type="text" name="News.NTitle" id="thenewsbt" />
</label></td>
<td>小标题:</td>
<td><label>
<input type="text" name="News.NShortTitle" id="thenewsxbt" />
</label></td>
</tr>
<tr>
<td>所属类型:</td>
<td><label>
<select name="News.NType" id="thenewslx">
<option value="省内新闻">省内新闻</option>
<option value="国内新闻">国内新闻</option>
<option value="国际新闻">国际新闻</option>
</select>
</label></td>
</tr>
<tr>
<td>包含图片:</td>
<td><label>
<select name="News.NHasPic" id="thenewsbhtp">
<option value="0">否</option>
<option value="1">是</option>
</select>
</label></td>
<td>上传图片:</td>
<td><label>
<input type="file" name="image" id="thenewstpwz" />
</label></td>
</tr>
</table>
<table width="900" border="1">
<tr>
<td>全文内容:</td>
</tr>
<tr>
<td><label>
<textarea name="News.NContent" id="thenewsnr" cols="45" rows="5"></textarea>
</label></td>
</tr>
<tr>
<s:submit id="thenewsadd" value="添加"></s:submit>
</tr>
</table>
<p> </p>
</s:form>

Upload代码:
public class UploadBean {
private String savePath;

public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}


public String Upload(File image,String name) throws Exception {
//获取文件上传后在服务器保存的位置
//upload文件夹用于保存文件
String path=
ServletActionContext.getServletContext()
.getRealPath("/upload");
//按照原文件名在upload文件夹下构建文件
File file=
new File(path+"//"+name);
//利用commons-io包中的FileUtiles类实现文件上传
FileUtils.copyFile(image, file);
this.setSavePath("upload/"+file);
System.out.println(savePath);
return savePath;
}

}
Action代码:
public class addNewsAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;

private News News=new News();
private UploadBean uploadBean=new UploadBean();
private String File;//文件

public String getFile() {
return File;
}

public void setFile(String file) {


File = file;
}

public UploadBean getUploadBean() {
return uploadBean;
}

public void setUploadBean(UploadBean uploadBean) {
this.uploadBean = uploadBean;
}

public News getNews() {
return News;
}

public void setNews(News news) {
News = news;
}

public String execute()throws Exception
{
BeanFactory bf = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
Dao dao = (Dao)bf.getBean("Daohibernate");
try{
Date date =new Date();
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//完整的时间
String time=sdf.format(date);
Timestamp NDate = Timestamp.valueOf(time);
News.setNDate(NDate);
System.out.println(News.getNDate());
File image=new File(File);
String NPic=uploadBean.Upload(image,image.getName());
News.setNPic(NPic);
dao.addNews(News);

}catch(Exception e){
this.addActionError("添加失败");
e.printStackTrace();
}
return SUCCESS;

}
}
我是新手,想将图片上传并保存地址到数据库,不知道怎么写,上面的代码不知道怎么实现不了...求各位大神帮忙啊!

[解决办法]
把客户端的图片上传到服务器?然后再把服务器图片的地址放到数据库
[解决办法]
来 给分呀,呵呵
[解决办法]
不建议直接将文件存入数据库,建议直接以文件形式存放在服务器磁盘。
存放在数据库必然会导致后期数据库更大。
并且对数据库进行文件读写性能更差
[解决办法]
我一般还是把地址放进数据库
[解决办法]

探讨
我一般还是把地址放进数据库

读书人网 >Java Web开发

热点推荐