文件上传不到数据库
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;//文件