Subversion Repositories SmartDukaan

Rev

Rev 3125 | Rev 4209 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3125 Rev 3364
Line 1... Line 1...
1
package in.shop2020.support.controllers;
1
package in.shop2020.support.controllers;
2
 
2
 
3
import in.shop2020.model.v1.catalog.Warehouse;
3
import in.shop2020.model.v1.catalog.Warehouse;
-
 
4
import in.shop2020.support.utils.FileUtils;
4
import in.shop2020.thrift.clients.CatalogClient;
5
import in.shop2020.thrift.clients.CatalogClient;
5
import in.shop2020.thrift.clients.HelperClient;
6
import in.shop2020.thrift.clients.HelperClient;
6
import in.shop2020.utils.LogisticsUser;
7
import in.shop2020.utils.LogisticsUser;
7
 
8
 
8
import java.io.ByteArrayOutputStream;
9
import java.io.ByteArrayOutputStream;
9
import java.io.File;
10
import java.io.File;
10
import java.io.FileInputStream;
-
 
11
import java.io.IOException;
11
import java.io.IOException;
12
import java.util.Calendar;
12
import java.util.Calendar;
13
import java.util.GregorianCalendar;
13
import java.util.GregorianCalendar;
14
import java.util.HashMap;
14
import java.util.HashMap;
15
import java.util.List;
15
import java.util.List;
Line 102... Line 102...
102
			response.setHeader("Content-disposition", "inline; filename=courier-details-" + deliveryType + "-" + warehouseId + "-" + providerId + "-" + year + "-"+ month + "-" + day +".xls" );
102
			response.setHeader("Content-disposition", "inline; filename=courier-details-" + deliveryType + "-" + warehouseId + "-" + providerId + "-" + year + "-"+ month + "-" + day +".xls" );
103
			
103
			
104
			ServletOutputStream sos;
104
			ServletOutputStream sos;
105
			try {
105
			try {
106
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
106
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
107
				baos.write(getBytesFromFile(new File(fileName)));
107
				baos.write(FileUtils.getBytesFromFile(new File(fileName)));
108
				sos = response.getOutputStream();
108
				sos = response.getOutputStream();
109
				baos.writeTo(sos);
109
				baos.writeTo(sos);
110
				sos.flush();
110
				sos.flush();
111
			} catch (IOException e) {
111
			} catch (IOException e) {
112
				logger.error("Unable to stream the courier details report", e);
112
				logger.error("Unable to stream the courier details report", e);
Line 148... Line 148...
148
     */
148
     */
149
	public String today(){
149
	public String today(){
150
		daysToSubtract = 0;
150
		daysToSubtract = 0;
151
		return show();
151
		return show();
152
	}
152
	}
153
	
-
 
154
    /**
-
 
155
     * Reads a file and converts its contents to a byte array.
-
 
156
     * 
-
 
157
     * @param file
-
 
158
     *            Name of the file to process
-
 
159
     * @return the contents of the file in a byte array.
-
 
160
     * @throws IOException
-
 
161
     *             if the file could not be found or read or is too big to
-
 
162
     *             convert to a byte array.
-
 
163
     */
-
 
164
	private static byte[] getBytesFromFile(File file) throws IOException {
-
 
165
	    FileInputStream is = new FileInputStream(file);
-
 
166
	    
-
 
167
	    // Get the size of the file
-
 
168
	    long length = file.length();
-
 
169
 
-
 
170
	    // You cannot create an array using a long type.
-
 
171
	    // It needs to be an int type.
-
 
172
	    // Before converting to an int type, check
-
 
173
	    // to ensure that file is not larger than Integer.MAX_VALUE.
-
 
174
	    if (length > Integer.MAX_VALUE) {
-
 
175
	        // File is too large
-
 
176
	        throw new IOException(file.getName() + " is too large to stream");
-
 
177
	    }
-
 
178
 
-
 
179
	    // Create the byte array to hold the data
-
 
180
	    byte[] bytes = new byte[(int)length];
-
 
181
 
-
 
182
	    // Read in the bytes
-
 
183
	    int offset = 0;
-
 
184
	    int numRead = 0;
-
 
185
	    while (offset < bytes.length
-
 
186
	           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
-
 
187
	        offset += numRead;
-
 
188
	    }
-
 
189
 
-
 
190
	    // Ensure all the bytes have been read in
-
 
191
	    if (offset < bytes.length) {
-
 
192
	        throw new IOException("Could not completely read file "+file.getName());
-
 
193
	    }
-
 
194
 
-
 
195
	    // Close the input stream and return bytes
-
 
196
	    is.close();
-
 
197
	    return bytes;
-
 
198
	}
153
		
199
	
-
 
200
	@Override
154
	@Override
201
	public void setServletContext(ServletContext context) {
155
	public void setServletContext(ServletContext context) {
202
		this.context = context;
156
		this.context = context;
203
	}
157
	}
204
	
158