Results 1 to 4 of 4
  1. #1
    mudit222 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Wink file upload in jsp

    how to upload file through a jsp page to a server???

  2. #2
    flyfisherman is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

  3. #3
    mudit222 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Red face 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???/

  4. #4
    flyfisherman is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by mudit222 View Post
    now can u tell me the code to download that file from server to client in jsp???/
    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);
    	}
    And here is the listing for fileupload.jsp file who will present images we collectet from servlets doGet

    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>
    Please notice that this code uses JSTL and has basic permission control.

Similar Threads

  1. how to upload a file using swing
    By ravi.virgoo@gmail.com in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2009, 06:49 AM
  2. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 01:21 AM
  3. how to upload a file?
    By tommy in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-30-2008, 03:50 PM
  4. file upload
    By sundarjothi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-27-2008, 12:52 PM
  5. File Upload
    By ShoeNinja in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-09-2007, 11:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •