Subversion Repositories SmartDukaan

Rev

Rev 5885 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5530 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.support.controllers;
5
 
6
import java.io.ByteArrayInputStream;
7
import java.io.File;
8
import java.io.IOException;
9
import java.text.SimpleDateFormat;
10
import java.util.Date;
11
 
12
import org.apache.commons.io.FileUtils;
13
import org.apache.struts2.dispatcher.StreamResult;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
 
17
import com.opensymphony.xwork2.ActionSupport;
18
 
19
/**
20
 * @author mandeep
21
 * 
22
 */
23
public class FileArchiveController extends ActionSupport {
24
    private static Logger logger = LoggerFactory
25
            .getLogger(FileArchiveController.class);
26
    private File file;
27
    private int fileType;
28
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
29
 
30
    public StreamResult create() {
31
        try {
32
            logger.info("Trying to archive file with type: " + fileType);
33
            FileUtils.copyFile(file, new File("/InventoryReports/" + fileType
34
                    + "-" + sdf.format(new Date()) + ".xls"));
35
 
36
            return new StreamResult(new ByteArrayInputStream(
37
                    ("File uploaded successfully at " + sdf.format(new Date())).getBytes()));
38
 
39
        } catch (IOException e) {
40
            logger.error("Error archiving file", e);
41
            return new StreamResult(new ByteArrayInputStream(
42
                    ("Could not upload file at " + sdf.format(new Date())).getBytes()));            
43
        }
44
    }
45
 
46
    public File getFile() {
47
        return file;
48
    }
49
 
50
    public void setFile(File file) {
51
        this.file = file;
52
    }
53
 
54
    public int getFileType() {
55
        return fileType;
56
    }
57
 
58
    public void setFileType(int fileType) {
59
        this.fileType = fileType;
60
    }
61
}