📄 opensightspotaction.java
字号:
/**
*
*/
package org.tshs.action.traveldeptaction;
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 javax.servlet.http.HttpSession;
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.actionform.traveldept.OpenSightspotForm;
import org.tshs.core.AuthenticationManager;
import org.tshs.entity.TravelCorp;
/**
*
* @author tiantian
*
*/
public final class OpenSightspotAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
Object obj = session.getAttribute("dept");
if (obj == null) {
request.setAttribute("message", "你的操作需要以旅游局身份登陆后才能执行!");
return mapping.findForward("dept.needlogin");
}
OpenSightspotForm osf = (OpenSightspotForm) form;
String name = osf.getName();
String address = osf.getAddress();
FormFile file = osf.getTheFile();
String discription = osf.getDescription();
String filePath = this.getServlet().getServletContext().getRealPath("/");
String extention = null;
String message = "";
if(name == null||name.trim().length()==0){
message += "景点名称不能为空;";
}else{
try {
if (AuthenticationManager.existSight(name)) {
message += "名为" + name + "的景点已经被注册; ";
}
} catch (Exception e1) {
e1.printStackTrace();
Vector v = new Vector();
v.add("景点添加失败,请稍后重试!");
request.setAttribute("errors", v);
return mapping.findForward("error");
}
}
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);
return mapping.findForward("failure");
}
if (file != null) {
try {
InputStream stream = file.getInputStream();// 把文件读入
String filename = file.getFileName();
extention = filename.substring(filename.lastIndexOf('.'));
File f = new File(filePath+"/upload");
if(!f.exists()){
f.mkdir();
}
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";
if(AuthenticationManager.openSightspot(filePath, name, address, extention, discription)){
pageForward="success";
request.setAttribute("message", "你已经成功添加这个景点!");
request.setAttribute("sight", AuthenticationManager.searchSightspot(name).get(0));
}
if(pageForward.equals("failure")){
request.setAttribute("message", "景点添加未成功,请稍后重试!");
}
return mapping.findForward(pageForward);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -