import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.FileUtils;
import cn.dip.model.Download;
import cn.dip.service.IDownloadService;
import cn.dip.util.DateUtil;
import cn.dip.util.JsonUtil;
public class DownloadAction extends BaseAction {
private IDownloadService downloadService;
private Integer id;
private File file;
private String fileFileName;
private String softname;
private int start;
private int limit;
public Integer getId() {
return id;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getSoftname() {
return softname;
}
public void setSoftname(String softname) {
this.softname = softname;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public void setId(Integer id) {
this.id = id;
}
public IDownloadService getDownloadService() {
return downloadService;
}
public void setDownloadService(IDownloadService downloadService) {
this.downloadService = downloadService;
}
public String getalldownloads() throws IOException {
List<Download> downloads = this.getDownloadService().findAll();
String jsonString = JsonUtil.pageFromListWithoutConfig(start, limit,
downloads);
PrintWriter pw = this.getResponse().getWriter();
pw.write(jsonString);
pw.flush();
pw.close();
return null;
}
public String download() throws IOException, ServletException{
Download download = this.getDownloadService().findById(this.getId());
String url = download.getUrl();
HttpServletResponse response = this.getResponse();
response.reset();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=\""+java.net.URLEncoder.encode(url, "UTF-8")+"\"");
this.getRequest().getRequestDispatcher(url).forward(this.getRequest(), this.getResponse());
return null;
}
//*****************************************************************
/**
* 得到所有的可供下载的软件
* @throws IOException
*/
public void showAll() throws IOException {
String result = "";
List list = downloadService.findAll();
if(null!=list&&list.size()>0){
result = JsonUtil.pageFromListWithoutConfig(start, limit, list);
}
else{
result = "{success:false}";
}
PrintWriter pw = getResponse().getWriter();
pw.write(result);
pw.flush();
pw.close();
}
/**
* 删除一个软件
* @throws IOException
*/
public void delete() throws IOException {
String result = "";
try{
Download dl = downloadService.findById(id);
if(null!=dl){
downloadService.delete(dl);
String url = dl.getUrl();
String targetDirectory = getRealPath() + "/download";
url = url.substring(url.lastIndexOf("/")+1);
System.out.println(url);
File file = new File(targetDirectory,url);
FileUtils.forceDelete(file);
}
result="{success:true}";
}catch(Exception e)
{
result="{success:false}";
}
PrintWriter pw = getResponse().getWriter();
pw.write(result);
pw.flush();
pw.close();
}
/**
* 上传一个软件
* @throws Exception
*/
public void upload() throws Exception {
String result="";
try{
System.out.println("进入方法");
String targetDirectory = getRealPath() + "/download";
System.out.println("targetDirectory:"+targetDirectory);
File uploadPath=new File(targetDirectory);
if(!uploadPath.exists()){
uploadPath.mkdir();
}
String targetFileName = generateFileName(fileFileName);
System.out.println("targetFileName:"+targetFileName);
File target = new File(targetDirectory, targetFileName);
InputStream in = new BufferedInputStream(new FileInputStream(file));
OutputStream ous = new BufferedOutputStream(new FileOutputStream(target));
System.out.println("复制文件开始");
Streams.copy(in, ous, true);
System.out.println("复制文件结束");
Download dl = new Download();
dl.setSoftname(fileFileName);
dl.setSoftsize(String.valueOf(bitToMb(file.length())));
dl.setTime(DateUtil.getDateAndTime());
dl.setUrl("download/" + targetFileName);
downloadService.save(dl);
result = "{success:true,message:'上传成功'}";
}catch(Exception e)
{
result = "{success:false,message:'上传失败'}";
}
HttpServletResponse response = this.getResponse();
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
pw.write(result);
pw.flush();
pw.close();
}
/**
* 将byte换算成MB
* @param length
* @return
*/
public String bitToMb(long length) {
float result = length / 1024;
String len = new DecimalFormat("###,###,###.##").format(result / 1024)
+ "MB";
return len;
}
/**
* 根据当前时间生成新的文件名称
* @param fileName
* @return
*/
private String generateFileName(String fileName) {
DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
}
}