这是jsp中的代码
文章标题
自定义属性
文章作者
发布时间
<div class="add-remark">
<span class="add-remark-sapn">内容摘要</span>
<textarea class="add-remark-textarea" name="remark" placeholder="请输入内容摘要部分"></textarea>
</div>
<div class="add-img">
<span class="add-img-span">封面图片</span>
<div class="add-img-div"><span>上传图片</span></div>
<input id="file" name="file" class="filepath" onchange="changepic(this)" type="file" />
<img src="images/0.jpg" id="show" class="srcimg">
</div>
<div class="add-content">
<span class="add-content-span">文章内容</span>
<textarea class="add-content-rextarea" name="contents">
</textarea>
</div>
<div class="add-opration">
<input type="submit" class="add-opration-submit" value="立即提交"/>
<input type="reset" class="add-opration-reset" value="重置" />
这是action中的代码
package com.aaa.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.swing.JOptionPane;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.aaa.entity.AAA_news;
import com.aaa.service.AAAservice;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class NewsAction extends ActionSupport implements ModelDriven{
private static final long serialVersionUID = 1L;
public AAAservice service;
private AAA_news news=new AAA_news();
private File file;
private String fileFileName;
private String fileContentType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public AAA_news getModel() {
return news;
}
public AAAservice getService() {
return service;
}
public void setService(AAAservice service) {
this.service = service;
}
public AAA_news getNews() {
return news;
}
public void setNews(AAA_news news) {
this.news = news;
}
public String saveNews() throws Exception {
System.out.println(news.getTitle()+news.getWriter()+news.getDate()
+news.getRemark()+news.getFile()+news.getContents());
String image="images";
JOptionPane.showMessageDialog(null,"44444..."+this.file);
if(file!=null){
String filePath=ServletActionContext.getServletContext().getRealPath(image);
String str=this.fileFileName;
String s=str.substring(str.length()-4,str.length());
String filename=image+"/"+news.getDate()+s;
try {
File flie=new File(filePath);
//获取运行目录
if(!flie.exists()){//检查文件是否存在,不存在自动创建
flie.mkdir();
}
FileUtils.copyFile(file, new File(filePath,filename));
news.setFile("images/"+filename);
this.service.saveNews(news);
} catch (Exception e) {
e.printStackTrace();
return NewsAction.INPUT;
}
}
return NewsAction.SUCCESS;
}
}
struts.xml中的配置
<action name="savenews" class="news" method="saveNews">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">application/pdf,text/plain,image/bmp,image/png</param>
<param name="maximumSize">5242880</param>
</interceptor-ref>
<result name="success">/main.jsp</result>
<result name="input">/addnews.jsp</result>
</action>