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

📄 uploadphoto.java

📁 新闻发布系统
💻 JAVA
字号:
package News.Upload;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class UploadPhoto extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static final int MAX_SIZE = 1024*1024*5; 
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletOutputStream out=null;
		DataInputStream in=null;
		FileOutputStream fileout=null;
		try{
			response.setContentType("text/plain;charset=gb2312");
			out=response.getOutputStream();
		}catch(IOException e){System.out.println(e.getStackTrace());return;}
		try{
			String contentType=request.getContentType();
			if(contentType!=null&&contentType.indexOf("multipart/form-data")!=-1)
			{
				in =new DataInputStream(request.getInputStream());
				int formDataLength = request.getContentLength(); 
				byte dataBytes[] = new byte[formDataLength]; 
				int bytesread=0;
				int totalbytesread=0;
				int sizeCheck=0;
				while(totalbytesread<formDataLength)
				{
					sizeCheck=totalbytesread+in.available();
					if(sizeCheck>MAX_SIZE)
					{
						out.println("<script>alert('对不起,您上传的文件长度超出最大值');history.go(-1);</script>");
						return;
					}
					bytesread=in.read(dataBytes,totalbytesread,formDataLength);
					totalbytesread+=bytesread;
				}
				String file = new String(dataBytes,"ISO-8859-1"); 
                               	dataBytes=null; 
				String saveFile = file.substring(file.indexOf("filename=\"")+1,file.indexOf("Content-Type:"));
				saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.lastIndexOf("\""));
				file = file.substring(file.indexOf("\n")+1);
				file = file.substring(file.indexOf("\n")+1);
                                file = file.substring(file.indexOf("\n")+1);
                                file = file.substring(file.indexOf("\n")+1,file.lastIndexOf("\n"));
		          	String fileName = new String(saveFile); 
				String extname=extname(fileName);
				if(!checkExtname(extname))
				{
					out.println("<script>alert('对不起,您上传的文件不合法');history.go(-1);</script>");
					return;
				}
				String newfilename=System.currentTimeMillis()+extname;
				fileout = new FileOutputStream(request.getRealPath("UploadPhoto")+"/"+newfilename); 
				fileout.write(file.getBytes("ISO-8859-1"),0,file.length()); 
				fileout.close();
                                request.getSession(true).setAttribute("filename",newfilename);
				response.sendRedirect("/News/Admin/Action.do?manner=optionAddNews");
			}
		}catch(Exception e){out.println("<script>alert('"+e.getMessage()+"');history.go(-1);</script>");}
		
	}
	
	public String extname(String fileName)
	{
		String filename=fileName.substring(fileName.lastIndexOf("."),fileName.length());
		return filename;
	}
	
	public boolean checkExtname(String extname)
	{
		String extName=extname.toLowerCase().substring(extname.indexOf(".")+1);
		String verify="|gif|jpg|png";
		StringTokenizer st=new StringTokenizer(verify,"|"); 
		while(st.hasMoreTokens())
		{
			String ext=st.nextToken();
			if(ext.equals(extName))
			{
				return true;
			}else{continue;}
		}
		return false;
	}

}

⌨️ 快捷键说明

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