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

📄 upfileservlet.java

📁 “JSP数据库项目案例导航”一书从第一章到第十一章各章实例的源程序文件以及数据库文件。 注意: 1. 本书中的案例提供的数据库环境不同
💻 JAVA
字号:
package oa.servlet;

import java.io.*;
import java.io.File;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
import oa.main.*;
import oa.bean.*;

public class UpFileServlet extends HttpServlet 
{
	private ServletConfig config;

	final public void init(ServletConfig config) throws ServletException 
	{
		this.config = config;
	}
	
	final public ServletConfig getServletConfig() 
	{
		return config;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doPost(request,response);
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html;charset=GBK");
		SmartUpload mySmartUpload = new SmartUpload();
		int count = 0;
		DealString ds = new DealString();
		oa.bean.SysmanBean mySysBean = null;
		try 
		{ 
			String strfile = "";
			String strname = "";
			String strtype1 = "";
			mySysBean = new oa.bean.SysmanBean();

			//初始化总大小
			if(mySysBean.getConn() == null){
				PrintWriter out = response.getWriter();
				out.println("<html>");
				out.println("<title>数据库无响应,请后退并重试</title>");
				out.println("数据库无法响应,请&nbsp;<a href='javascript:window.history.back(-1);'>返回</a>&nbsp;重试");
				out.println("</html>");
				out.close();
				return;
			}
			mySysBean.setType(1);
			mySysBean.setID("1");
			mySysBean.setDictname("发表文章图片大小");
			Hashtable hash1 = (Hashtable)mySysBean.getOneData();
			String str_size = (String)hash1.get("XMMC");
			String strDisp = str_size;
			str_size = str_size.substring(0,str_size.length()-2);
			int sumsize = Integer.parseInt(str_size)*1024*1024;
			//判断大小是否超过


			mySmartUpload.initialize(config,request,response);

			//1.限制每个上传文件的最大长度。
			mySmartUpload.setMaxFileSize(sumsize);

			mySmartUpload.upload();	

			java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames();

			//取各种参数
			while (e.hasMoreElements()) 
			{
				String key = (String)e.nextElement();
				String[] values = mySmartUpload.getRequest().getParameterValues(key);
			
				for(int i = 0; i < values.length; i++)
				{
					if(key.equals("txt_file11")){
						strfile=ds.toString(values[i]);
						}
					if(key.equals("txt_name11")){
						strname=ds.toString(values[i]);
						}
					if(key.equals("txt_type1"))
						strtype1=values[i];
				}
			}
			//System.out.println("++++"+strfile+"-----"+strname);
			//上传到服务器的路径
			String saveDirectory = "";
			if(strtype1.equals("1"))
				saveDirectory = oa.main.ParentBean.FilesPath+"news/appendix/";
			if(strtype1.equals("2"))
				saveDirectory = oa.main.ParentBean.FilesPath+"news/img/";
			if(strtype1.equals("3"))
				saveDirectory = oa.main.ParentBean.FilesPath+"news/img/";
			if(strtype1.equals("4"))
				saveDirectory = oa.main.ParentBean.FilesPath+"photo\\";
			
			
			
			
			

	//初始化年月
	String upmonth = ds.getDateTime();

//cancel photo
if(!strtype1.equals("4")){	
	//新建目录
  	java.io.File f = new java.io.File(saveDirectory+upmonth.substring(0,4));
	if(!f.exists())
	{
		f.mkdir();
	}
	f = new java.io.File(saveDirectory+upmonth.substring(0,4)+"/"+upmonth.substring(5,7));
	if(!f.exists())
	{
		f.mkdir();
	}

	f = new java.io.File(saveDirectory+upmonth.substring(0,4)+"/"+upmonth.substring(5,7)+"/"+upmonth.substring(8,10));
	if(!f.exists())
	{
		f.mkdir();
	}
}//end if



	String subPath=new String(upmonth.substring(0,4)+"/"+upmonth.substring(5,7)+"/"+upmonth.substring(8,10)+"/");

	//cancel "photo"
	if(!strtype1.equals("4")) saveDirectory=saveDirectory+subPath;

	//System.out.println(saveDirectory);
			//保存文件
			count = mySmartUpload.save(saveDirectory);	

			int i = strfile.lastIndexOf("\\");
			strfile = strfile.substring(i+1);
			String savefile = saveDirectory+strfile;//旧名

			String savefile1 = saveDirectory+strname;//新名
			File f2 = new File(savefile1);
			if(f2.exists())//新名文件存在
			{
				f2.delete();
			}

			File f1 = new File(savefile);
			f1.renameTo(new File(savefile1)); 	
			
			if(!strtype1.equals("4"))
				response.sendRedirect("oa/manage/upload.jsp?close=1&id="+strtype1+"&filename="+ds.toUtf8String(subPath+strname));
			else
				response.sendRedirect("oa/organization/upload.jsp?close=1&id="+ds.toASCII(strname)+"&filename="+ds.toUtf8String(subPath+strname));
		}catch (Exception e){			
			(response.getOutputStream()).println("<html><body><input type=button value='返回' onclick='window.close();'><br><span align='center'>错误:网络连接错误或非法的上传文件,请检查您上传的文件类型或大小!"+e.getMessage()+"</span><br><br><script>window.opener.document.all.img.value=''</script></body></html>");
		}finally{
			if(mySysBean!=null)
				mySysBean.closeConn();
		}
    }	

	public void  destroy () 
	{
	}
} 

⌨️ 快捷键说明

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