|
news.jsp
<form action="<%=path %>/news.do?method=add" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="${news.id }">
<table width="90%" border="0" align="left" cellpadding="0" cellspacing="1" class="newTable">
<tr>
<td class="newTd">新闻标题</td>
<td class="newTd"><input type="text" name="title" size="30"></td>
</tr>
<tr>
<td class="newTd">新闻副标题</td>
<td class="newTd"><input type="text" name="subTitle" size="30"></td>
</tr>
<tr>
<td class="newTd">新闻内容</td>
<td class="newTd"><textarea class="ckeditor" name="content"></textarea></td>
</tr>
<tr>
<td class="newTd">新闻图片</td>
<td class="newTd" id="more"><input type="file" name="uploadFile[0].file" size="30"><input type="button" value="增加"></td>
</tr>
<tr>
<td class="newTd">新闻所属板块</td>
<td class="newTd">
<select id="modelSelect" name="modelId">
</select>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="保留">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
<script type="text/javascript">
var t = 1;
function addFile()
{
var parent = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type = "file";
input.name = "uploadFile[" + (t++) + "].file";
input.size = "30";
button.type = "button";
button.value = "删除";
button.onclick = function()
{
parent.removeChild(br);
parent.removeChild(input);
parent.removeChild(button);
};
parent.appendChild(br);
parent.appendChild(input);
parent.appendChild(button);
}
</script>
newsForm.java
package com.ahhffl.struts.forms;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
public class NewsForm extends ActionForm{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String title;
private String subTitle;
private String content;
private String modelId;
private List myFiles;
public List getMyFiles() {
return myFiles;
}
public void setMyFiles(List myFiles) {
this.myFiles = myFiles;
}
public NewsForm() {
myFiles = new ArrayList();
}
// 留意这个方法的定义 不加中间的轮回是会犯错的
public UploadFile getUploadFile(int index) {
int size = myFiles.size();
if (index > size - 1) {
for (int i = 0; i < index - size + 1; i++) {
myFiles.add(new UploadFile());
}
}
return (UploadFile) myFiles.get(index);
}
public String getModelId() {
return modelId;
}
public void setModelId(String modelId) {
this.modelId = modelId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
UploadFile.java
package com.ahhffl.struts.forms;
import java.io.Serializable;
import org.apache.struts.upload.FormFile;
public class UploadFile implements Serializable {
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
NewsAction.java
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
NewsForm newsForm = (NewsForm)form;
News news = new News();
news.setTitle(newsForm.getTitle());
news.setSubTitle(newsForm.getSubTitle());
news.setContent(newsForm.getContent());
news.setCreateDate(new Date());
String modelId = request.getParameter("modelId");
Model model = (Model)companyService.getObject(Model.class, modelId);
news.setModel(model);
//保留图片
String bathPath = request.getSession().getServletContext().getRealPath("/") + "news";
//System.out.println(bathPath);
StringBuffer sb = new StringBuffer();
List myFiles = newsForm.getMyFiles();
for (int i = 0; i < myFiles.size(); i++) {
UploadFile uploadFile = (UploadFile) myFiles.get(i);
FormFile file = uploadFile.getFile();
String fileType = file.getFileName().substring(file.getFileName().lastIndexOf(".") + 1, file.getFileName().length());
String fileName = FileUtil.getRandomFileName() + "." + fileType;
if(file.getFileSize() > 0){
FileUtil.saveFile(file.getInputStream(), bathPath , fileName);
}
sb.append("/news/" + fileName + ";");
}
news.setPicturePath(sb.toString());
companyService.addObject(news);
return mapping.findForward("add");
}
FileUtil.java
//随即天生文件名
public static String getRandomFileName() {
StringBuffer buffer =
new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
StringBuffer sb = new StringBuffer();
Random r = new Random();
int range = buffer.length();
for (int i = 0; i < 10; i ++) {
sb.append(buffer.charAt(r.nextInt(range)));
}
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddhhmmss");
String strCreateTime = fmt.format(new Date().getTime()); //文件创建日期
return strCreateTime + sb.toString();
}
/**
* 保留文件
*/
public static void saveFile(InputStream stream, String filePath, String fineName) {
OutputStream bos = null; //文件输出流
try {
File file = new File(filePath);
if(!file.exists())
{
file.mkdirs();
}
file = new File(filePath , fineName);
bos = new FileOutputStream(file); //通过输出流构建文件
int bytesRead = 0; //阅读输入文件流的标识
byte[] buffer = new byte[1024];
//读取输入流
while ((bytesRead = stream.read(buffer, 0, buffer.length)) != -1){
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
} catch(Exception e) {
} finally {
try {
if(bos != null) bos.close();
if(stream != null) stream.close();
} catch (Exception e1) {
}
}
} |
|