經過smartupload插件上傳java
1:下載插件並導入jsp
右擊項目àbuild pathàconfigue build pathàlibrariesàadd jarsàui
2:java代碼spa
public class Smart extends HttpServlet {插件
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {對象
doPost(req, resp);圖片
}get
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {it
//設置上傳文件保存路徑io
String filePath = getServletContext().getRealPath ("/") + "images";
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
SmartUpload su = new SmartUpload();
//初始化對象
su.initialize(getServletConfig(), req, resp);
//設置上傳文件大小
su.setMaxFileSize(1024*1024*10);
//設置全部文件的大小
su.setTotalMaxFileSize(1024*1024*100);
//設置容許上傳文件類型
su.setAllowedFilesList("txt,jpg,gif");
String result = "上傳成功!";
//設置禁止上傳的文件類型
try {
su.setDeniedFilesList("rar,jsp,js");
//上傳文件
su.upload();
int count = su.save(filePath);
System.out.println("上傳成功" + count + "個文件!");
}
catch (Exception e) {
result = "上傳失敗!";
e.printStackTrace();
}
req.setAttribute("result",result);
req.getRequestDispatcher("up.jsp").forward(req, resp);
}
}
2:下載
Jsp代碼:(圖片保存在images目錄下)
<!--下載: <a href="Smartdown?filename=005.jpg">005.jpg</a>
--> <hr>
Java代碼
public class Smartdown extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String filename=request.getParameter("filename");
SmartUpload su=new SmartUpload();
su.setContentDisposition(null);
su.initialize(getServletConfig(), request, response);
try {
su.downloadFile("/images/"+filename);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}