⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadaction.java

📁 在线读书交流平台
💻 JAVA
字号:
package com.olr.control.space;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.olr.BO.SpaceBO;
import com.olr.beans.Book;
import com.olr.beans.User;
import com.olr.control.common.BaseAction;

public class UploadAction extends BaseAction {
	    private static final long serialVersionUID = 572146812454l ;
	    private static final int BUFFER_SIZE = 16 * 1024 ;
	   
	    private File myFile;
	    private String contentType;
	    private String fileName;
	    private String imageFileName;
	    private int bookId;
	    private User user;
	    private Book works;
	    private SpaceBO spaceBO;
	    private String imagePath;
	    
	
		Logger log = Logger.getLogger(this.getClass());  
	    public void setMyFileContentType(String contentType) {
	        this .contentType = contentType;
	   }
	   
	    public void setMyFileFileName(String fileName) {
	        this .fileName = fileName;
	   }
	       
	    public void setMyFile(File myFile) {
	        this .myFile = myFile;
	   }
	   
	    public String getImageFileName() {
	        return imageFileName;
	   }
	
	    public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}
		   
	

	   
	    private static void copy(File src, File dst) {
	        try {
	           InputStream in = null ;
	           OutputStream out = null ;
	            try {                
	               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
	               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
	                byte [] buffer = new byte [BUFFER_SIZE];
	                while (in.read(buffer) > 0 ) {
	                   out.write(buffer);
	               }
	           } finally {
	                if ( null != in) {
	                   in.close();
	               }
	                if ( null != out) {
	                   out.close();
	               }
	           }
	       } catch (Exception e) {
	          
	       }
	   }
	   
	    private static String getExtention(String fileName) {
	        int pos = fileName.lastIndexOf( "." );
	        return fileName.substring(pos);
	   }

	 
	    public String userImage(){
		   User user=this.getSpaceBO().getUser(this.getSession("spaceOwnerName").toString());
		   this.setUser(user);
	       imageFileName = user.getUsername()+ getExtention(fileName);
	       imagePath=imageFileName;
	       user.setImagePath("UploadImages"+"/user/"+imageFileName);
	       this.getSpaceBO().updateUserInfo(user);
	       File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages") + "/user/" + imageFileName);
	       copy(myFile, imageFile);
	       log.info(user.getUsername()+"上传头像成功");
	        return "user";
	   }
	    
	    public String bookImage(){
			   works=this.getSpaceBO().getWorks(bookId);
			   this.setWorks(works);
		       imageFileName = works.getBookId()+ getExtention(fileName);
		       imagePath=imageFileName;
		       works.setImagePath("UploadImages"+"/book/"+imageFileName);
		       this.getSpaceBO().updateWorks(works);
		       File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages") + "/book/" + imageFileName);
		       copy(myFile, imageFile);
		       log.info(works.getBookname()+"上传头像成功");
		        return "book";
		   }

	public SpaceBO getSpaceBO() {
		return spaceBO;
	}

	public void setSpaceBO(SpaceBO spaceBO) {
		this.spaceBO = spaceBO;
	}

	public Book getWorks() {
		return works;
	}

	public void setWorks(Book works) {
		this.works = works;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
}

	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -