你可以使用SmartUpload,来实现这个功能,SmartUpload这个
你可以这样做:
addpic.jsp
<html><body>
<FORM METHOD="POST" ACTION="/uploadserlvet" ENCTYPE="multipart/form-data">
<input name="file1" type="file">
<input type="submit" name="Submit" value="图片添加">
</FORM>
</body>
</html>
servlet的写法如下:
uploadservlet.java: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
// Variables
int count=0;
SmartUpload mySmartUpload = new SmartUpload();
try {
strfilePath="/upload/";//文件上传后的相对路径
// Initialization
mySmartUpload.initialize(config,request,response);
mySmartUpload.setAllowedFilesList"JPG,JPEG,GIF,BMP,jpg,jpeg,gif,bmp");//上传图片文件的扩展名的限制
mySmartUpload.setMaxFileSize(100000000);//上传文件的最大字节数
// Upload
mySmartUpload.upload();
//strFilepath=m_application.getRealPath(strfilePath); if (!mySmartUpload.getFiles().getFile(i).isMissing()){
String strFileName= mySmartUpload.getFiles().getFile(i).getFileName();
count = mySmartUpload.save(strfilePath);
}
}
request.setAttribute("errorInfo",strFileName);
getServletContext().getRequestDispatcher("/showpic.jsp").forward(request,response);
return;
}catch(Exception e){
String[] strRet=new String[3];
strRet[0]="ERROR[-1]";
strRet[1]="不能上传文件!";
strRet[2]="";
request.setAttribute("picFileName",strRet);
getServletContext().getRequestDispatcher("/addpic.jsp").forward(request,response);
return;
}finally{
}
}
在showpic.jsp页面中接受上传图片的名字
如:
<html>
<body>
<%String FileName=(String)request.getAttribute("picFileName");%>
<img src="/upload/<%=FileName%>">
</body>
</html>
你可以这样做:
addpic.jsp
<html><body>
<FORM METHOD="POST" ACTION="/uploadserlvet" ENCTYPE="multipart/form-data">
<input name="file1" type="file">
<input type="submit" name="Submit" value="图片添加">
</FORM>
</body>
</html>
servlet的写法如下:
uploadservlet.java: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
// Variables
int count=0;
SmartUpload mySmartUpload = new SmartUpload();
try {
strfilePath="/upload/";//文件上传后的相对路径
// Initialization
mySmartUpload.initialize(config,request,response);
mySmartUpload.setAllowedFilesList"JPG,JPEG,GIF,BMP,jpg,jpeg,gif,bmp");//上传图片文件的扩展名的限制
mySmartUpload.setMaxFileSize(100000000);//上传文件的最大字节数
// Upload
mySmartUpload.upload();
//strFilepath=m_application.getRealPath(strfilePath); if (!mySmartUpload.getFiles().getFile(i).isMissing()){
String strFileName= mySmartUpload.getFiles().getFile(i).getFileName();
count = mySmartUpload.save(strfilePath);
}
}
request.setAttribute("errorInfo",strFileName);
getServletContext().getRequestDispatcher("/showpic.jsp").forward(request,response);
return;
}catch(Exception e){
String[] strRet=new String[3];
strRet[0]="ERROR[-1]";
strRet[1]="不能上传文件!";
strRet[2]="";
request.setAttribute("picFileName",strRet);
getServletContext().getRequestDispatcher("/addpic.jsp").forward(request,response);
return;
}finally{
}
}
在showpic.jsp页面中接受上传图片的名字
如:
<html>
<body>
<%String FileName=(String)request.getAttribute("picFileName");%>
<img src="/upload/<%=FileName%>">
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货