?? uploadphoto.java
字號(hào):
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('對(duì)不起,您上傳的文件長(zhǎng)度超出最大值');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('對(duì)不起,您上傳的文件不合法');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;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -