📄 updatesightaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.tshs.struts.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.tshs.core.AuthenticationManager;
import org.tshs.entity.SightSpot;
import org.tshs.struts.form.UpdateSightForm;
/**
* MyEclipse Struts
* Creation date: 12-18-2006
*
* XDoclet definition:
* @struts.action path="/updateSight" name="updateSightForm" input="/form/updateSight.jsp" scope="request" validate="true"
* @struts.action-forward name="success" path="/dept/sightFormdiplay.jsp"
* @struts.action-forward name="failure" path="/dept/modifysightForm.jsp"
*/
public class UpdateSightAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Object obj = request.getSession().getAttribute("dept");
if (obj == null) {
request.setAttribute("message", "你的操作需要以旅游局身份登陆后才能执行!");
return mapping.findForward("dept.needlogin");
}
UpdateSightForm sightForm = (UpdateSightForm) form;
String tid = sightForm.getId();
String name=sightForm.getName();
String address=sightForm.getAddress();
FormFile file = sightForm.getImage();
String discription = sightForm.getDescription();
String filePath = this.getServlet().getServletContext().getRealPath("/");
String extention = null;
String message = "";
if(name == null||name.trim().length()==0){
message = "景点名称不能为空;";
}
if(address==null||address.trim().length()==0){
message += "景点地址不能为空;";
}
if(discription==null||discription.trim().length()==0){
message += "景点简介不能为空;";
}
if(file == null||file.getFileSize()==0){
message += "上传相片不能为空,重新上传!";
}
if(message.length()>0){
request.setAttribute("message", message);
Vector v = new Vector();
v.add(tid);
v.add(name);
v.add(address);
v.add(discription);
request.setAttribute("information", v);
return new ActionForward("/dept/modifysight.jsp");
}
try {
InputStream stream = file.getInputStream();// 把文件读入
String filename = file.getFileName();
extention = filename.substring(filename.lastIndexOf('.'));
File dir = new File(filePath+"/upload");
if(!dir.exists()){
dir.mkdirs();
}
File toFile = new File(filePath + "/upload/temp" + extention);
/*if(!toFile.exists()){
toFile.createNewFile();
}*/
System.out.println(toFile.getAbsolutePath());
OutputStream bos = new FileOutputStream(toFile);// 建立一个上传文件的输出流
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件写入服务器
}
bos.close();
stream.close();
} catch (Exception e) {
System.err.print(e);
}
String pageForward="failure";
Long id = Long.valueOf(sightForm.getId());
System.out.println(id);
if(AuthenticationManager.UpdateSightspot(id,filePath, name, address, extention, discription)){
pageForward="success";
SightSpot sight = (SightSpot) AuthenticationManager.getById(id);
request.setAttribute("sight", sight);
request.setAttribute("message", "景点信息更新成功!");
}
return mapping.findForward(pageForward);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -