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

📄 uploadaction.java

📁 类似于csdn的上传下载系统
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.adam.struts.action;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.adam.bean.DB;
import org.adam.bean.Download;
import org.adam.struts.form.UploadForm;
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;

/** 
 * MyEclipse Struts
 * Creation date: 08-11-2008
 * 
 * XDoclet definition:
 * @struts.action path="/upload" name="uploadForm" scope="request"
 */
public class UploadAction 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)throws Exception {
		UploadForm upload = (UploadForm) form;// TODO Auto-generated method stub
		String ends=null;
		String store_path = servlet.getServletContext().getRealPath("/fileupload");//设置存放路径
    	//System.out.println(store_path);
    	HttpSession session=request.getSession();
    	
    	FormFile file = upload.getFile();
    	
    	if (file == null){
    		return mapping.findForward("index");
    	}
    	
    	//String filename = session.getAttribute("username").toString()+file.getFileName();
    	String oldfilename =file.getFileName();
    	//System.out.println(oldfilename);
    	int length=oldfilename.length();
    	if(oldfilename.substring(oldfilename.length()-5).equals(".java")||oldfilename.substring(oldfilename.length()-5).equals(".html")){
    		ends=oldfilename.substring(length-5);
    	}else if(oldfilename.substring(length-3).equals(".js")){
    		ends=oldfilename.substring(length-3);
    	}else{
    		ends=oldfilename.substring(length-4);
    	}
    	
    	String filename=new Date().toLocaleString().substring(5, 17).replaceAll(":", "-")+upload.getFilename()+ends;
    	upload.setFilename(filename);
    	//System.out.println(filename);
    	String size = Integer.toString(file.getFileSize()) + "bytes";
    	upload.setSize(size);
    	
    	InputStream is = file.getInputStream();
    	OutputStream os = new FileOutputStream(store_path + "/" + filename);
    	
    	int bytes = 0;
    	byte [] buffer = new byte[8192];
    	while ((bytes = is.read(buffer,0,8192))!=-1){
    		os.write(buffer,0,bytes);
    	}
    	
    	
    	os.close();
    	is.close();
    	file.destroy();
    	
    	ServletContext context=servlet.getServletContext();
		DataSource ds=(DataSource)context.getAttribute("download");
		DB db=new DB(ds);
		Download down=new Download();
		down.setDownname(filename);
		down.setIntroduction(upload.getIntroduction().replaceAll("\n","<br>"));
		down.setVisitcount(0);
		down.setSort_id(Integer.parseInt(upload.getSort_id()));
		down.setUsertable_id(Integer.parseInt(session.getAttribute("User_ID").toString()));
		down.setUploadtime(new Date().toLocaleString());
		down.insertdownload(db);
		return mapping.findForward("upload");
	}
}

⌨️ 快捷键说明

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