ファイルアップロード表示データベースインプリメンテーションクラスに保存
8152 ワード
public class UpLoadAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private List<File> uploadFile;
private List<String> uploadFileContentType;
private List<String> uploadFileFileName;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
/**
* ,
* @return
* @throws Exception
*/
public String uploadImage() throws Exception {
String savePath = request.getSession().getServletContext().getRealPath("/upload/temp");
String result = "error";
File dir = new File(savePath);
if (!dir.exists()) {
dir.mkdirs();
}
List<File> files = getUploadFile();
if((files!=null) && (files.size()>0)){
for (int i = 0; i < files.size(); i++) {
FileUtils.copyFile(files.get(i), new File(savePath + "//"
+ getUploadFileFileName().get(i)));
}
}
else{
return result;
}
return SUCCESS;
}
/**
*
* @return
*/
public String getHotelImage(){
try {
String name = request.getParameter("name");
name = request.getSession().getServletContext().getRealPath("/") +"upload/hotel/" + name;
FileInputStream fi = new FileInputStream(name);
BufferedInputStream bi = new BufferedInputStream(fi);
int c =0;
while((c=bi.read())!=-1){
response.getWriter().write(c);
}
response.getWriter().flush();
fi.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
/**
*
* @return
*/
public String insertiImageInfo() {
try{
hotelimage = new HotelImage();
hotelimage.setImageTitle(imageTitle);
String image = request.getParameter("uploadFileName");
hotelimage.setImageName(request.getParameter("oldUploadFileName"));
String savePath = request.getSession().getServletContext().getRealPath("/upload");
try {
hotelimage.setImagePath(URLEncoder.encode( image,"utf-8"));
FileUtils.copyFile(new File(savePath+"/temp/" + hotelimage.getImagePath()), new File(savePath+"/hotel/" + hotelimage.getImagePath()));
} catch (Exception e) {
e.printStackTrace();
}
HttpSession session = request.getSession();
//
if(session.getAttribute("AdminSession")!=null){
UserInfo userInfo = (UserInfo) session.getAttribute("AdminSession");
if(userInfo!=null){
hotelimage.setUploadUserId(Integer.parseInt(userInfo.getId()));
}
}
else{
hotelimage.setUploadUserId(0);
}
hotelimage.setHotelId(hotelId);
hotelimage.setImageType(imageType);
hotelimage.setImageDesc(content);
int t = hotelImageService.addHotelImageService(hotelimage);
if(t<=0){
return "error";
}
hotelId="0";
imageType="0";
imageTitle="";
}
catch(Exception ex){
ex.printStackTrace();
return "error";
}
return "success";
}
public String goEditImage(){
String id = request.getParameter("id");
HotelImage image = hotelImageService.getImageByID(Long.valueOf(id));
List<HotelInfo> hotels = hotelImageService.queryHotelListService(hotelInfo);
request.setAttribute("hotels", hotels);
request.setAttribute("image", image);
if("audit".equals(request.getParameter("type"))){
return "audit";
}
return "success";
}
public String editImage() {
try{
hotelimage = new HotelImage();
hotelimage.setImageTitle(imageTitle);
String image = request.getParameter("uploadFileName");
hotelimage.setImageName(request.getParameter("oldUploadFileName"));
String savePath = request.getSession().getServletContext().getRealPath("/upload");
try {
hotelimage.setImagePath(URLEncoder.encode( image,"utf-8"));
if(!"0".equals(request.getParameter("upload"))){
FileUtils.copyFile(new File(savePath+"/temp/" + hotelimage.getImagePath()), new File(savePath+"/hotel/" + hotelimage.getImagePath()));
}
} catch (Exception e) {
e.printStackTrace();
}
HttpSession session = request.getSession();
//
if(session.getAttribute("AdminSession")!=null){
UserInfo userInfo = (UserInfo) session.getAttribute("AdminSession");
if(userInfo!=null){
hotelimage.setUploadUserId(Integer.parseInt(userInfo.getId()));
}
}
else{
hotelimage.setUploadUserId(0);
}
hotelimage.setHotelId(hotelId);
hotelimage.setImageType(imageType);
hotelimage.setImageDesc(content);
hotelimage.setImageId(imageId);
int t = hotelImageService.updateImage(hotelimage);
if(t<=0){
return "error";
}
hotelId="0";
imageType="0";
imageTitle="";
}
catch(Exception ex){
ex.printStackTrace();
return "error";
}
return ActionSupport.SUCCESS;
}
public String auditImage() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
UserInfo userInfo = (UserInfo) session.getAttribute("AdminSession");
HotelImage image =new HotelImage();
image.setImageId(imageId);
image.setAudit_flag(request.getParameter("audit_flag"));
image.setAudit_fault_reson(request.getParameter("audit_fault_reason"));
image.setAudit_user(Integer.valueOf(userInfo.getId()));
hotelImageService.auditImage(image);
return ActionSupport.SUCCESS;
}
public String deleteImage(){
HttpServletRequest request = ServletActionContext.getRequest();
String [] ids = request.getParameterValues("checkedIds");
String newsIds = StringUtils.join(ids, ",");
hotelImageService.deleteImageByIds(newsIds);
return "success";
}
public String getGallerialImage(){
HotelImage image = new HotelImage();
image.setStartRow(1);
image.setPageSize(20);
String path = request.getContextPath() +"/upload/hotel/";
List<HotelImage> list = hotelImageService.getImageList(image);
List<GalleriaImage> galleriaImages = new ArrayList<GalleriaImage>();
GalleriaImage galleriaImage = null;
for(int i=0;i<list.size();i++){
galleriaImage = new GalleriaImage();
image = list.get(i);
galleriaImage.setDescription(image.getImageDesc());
galleriaImage.setImage(path+image.getImagePath());
galleriaImage.setTitle(image.getImageTitle());
galleriaImages.add(galleriaImage);
}
JSONArray jo = JSONArray.fromObject(galleriaImages);
json = jo.toString();
return "success";
}
}
//getter setter