Results 1 to 4 of 4
Thread: file upload in jsp
- 04-18-2010, 09:10 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
- 04-19-2010, 01:16 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 26
- Rep Power
- 0
You can use instructions from this link
AJAX File Upload Progress for Java - CodeGuru
or this
jGuru: How can I read parameters from a multipart/form-data upload form?
- 04-20-2010, 07:58 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
urgent question related to file downloading
i have put a question ......how to upload file through a jsp page to a server??? and i got best answer from your team ...........ans. was ajax file upload..........
now can u tell me the code to download that file from server to client in jsp???/
- 04-20-2010, 08:10 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 26
- Rep Power
- 0
Well that depends from the upload location. If you upload files in public accessible locations, you will need a few lines of code to read dir content and generate links to every file...
For example here are the few lines I wrote for this purpose
FileUploadServlet
Java Code:protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); Integer userId = (Integer) session.getAttribute("LoggedInUserId"); ServletContext sc = getServletContext(); String idString = request.getParameter("id"); String upload_location = request.getParameter("upload_location"); String product_path = null; if (upload_location.equals("products")) product_path = sc.getRealPath("products/"); // dir za slike else product_path = sc.getRealPath("computers/"); // dir za slike String url = ""; if (userId == null || idString == null) { url = "/index.jsp"; } else { int id = Integer.parseInt(idString); if (UserDB.isAdmin(userId) && (id > 0)) { session.setAttribute("productId", id); session.setAttribute("upload_location", upload_location); url = "/fileupload.jsp"; } else { url = "/index.jsp"; } session.setAttribute("productId", id); ArrayList<String> images = new ArrayList<String>(); File folder = new File(product_path + "/" + id + "/mid/"); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { images.add(listOfFiles[i].getName()); //System.out.println("File " + listOfFiles[i].getName()); } } session.setAttribute("images", images); } // forward ka JSP stranici RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); dispatcher.forward(request, response); }
Java Code:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <c:if test="${LoggedInUserGroup == 1}"> <head> <script type="text/javascript" src="template/js/js.js"></script> <title>Upload slike</title> </head> <body> <div align="center"> <form id="myForm" enctype="multipart/form-data" method="post" action="FileUpload"> <input type="hidden" name="dir" value="<c:out value="${productId}"/>" /> <input type="hidden" name="upload_location" value="<c:out value="${upload_location}"/>" /> <table border="0" cellspacing="5" width="60%"> <tr> <td align="right" width="60%"> <input type="file" name="txtFile" id="txtFile" /> </td> <td align="left" width="40%"><input type="submit" id="submitID" name="submit" value="Upload" /></td> </tr> <c:forEach var="images" items="${images}"> <tr> <td align="center"> <c:if test="${(upload_location == 'products') || (upload_location == 'computers')}"> <a href="<c:out value="${upload_location}"/>/<c:out value="${productId}"/>/<c:out value="${images}"/>" title="<c:out value="${product.name}"/>"> <img src="<c:out value="${upload_location}"/>/<c:out value="${productId}"/>/mid/<c:out value="${images}"/>" border="0" /> </a> </c:if> </td> <td align="left"> <a href="javascript: if(confirm('Da li ste sigurni?')) idi_na('DeleteFile?fajl=<c:out value="${images}"/>')" title="Brisanje slike"> <img src="template/images/delete.png" border="0" /> </a></td> </tr> <tr> <td align="center"><a href="javascript:location.reload(true)">Refresh</a></td> <td></td> </tr> </c:forEach> </table> </form> </div> </body> </c:if> </html>
Similar Threads
-
how to upload a file using swing
By ravi.virgoo@gmail.com in forum AWT / SwingReplies: 2Last Post: 11-20-2009, 05:49 AM -
:large file upload to server(chunk upload)
By tommy_725 in forum NetworkingReplies: 0Last Post: 10-16-2009, 12:21 AM -
how to upload a file?
By tommy in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 06-30-2008, 02:50 PM -
file upload
By sundarjothi in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-27-2008, 11:52 AM -
File Upload
By ShoeNinja in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-09-2007, 10:09 PM
Bookmarks