文件下载,带转码->pdf->swf

时间:2023-03-08 16:07:58

private String upload = "保存的路径";

//文件下载

public String download() {

//初始化

this.initContext();

String apath = request.getSession().getServletContext().getRealPath(upload);



File af = new File(apath);

// 假设没有这个目录。就创建个目录

if (!af.exists()) {

af.mkdirs();

}





String filenewaddress = request.getSession().getServletContext().getRealPath(upload);

    

System.out.println("開始下载,请稍后……");

URL urlfile = null;

HttpURLConnection httpUrl = null;

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {



// 把下载地址赋给urlfile 

urlfile = new URL(fileoldaddress);

// 打开下载 URL 引用的资源的通信链接(假设尚未建立这种连接)

httpUrl = (HttpURLConnection) urlfile.openConnection();

// 建立与指定socket的连接

httpUrl.connect();

// 提供一个默认的文件名称

String context = httpUrl.getHeaderField("Content-Disposition");

// 推断context是否为空

if (context == null) {

// 取地址最后的/后面的字符当做名字

filename1 = fileoldaddress.substring(fileoldaddress

.lastIndexOf("/") + 1, fileoldaddress.length());

filenewaddress = filenewaddress + "\\" + filename1;

} else {

filename1 = context.substring(context.lastIndexOf("=") + 1,

context.length());

filenewaddress = filenewaddress + "\\" + filename1;

}

// 把本地保存地址赋给f

File f = new File(filenewaddress);

bis = new BufferedInputStream(httpUrl.getInputStream());

bos = new BufferedOutputStream(new FileOutputStream(f));

int len = 2048;

byte[] b = new byte[len];

while ((len = bis.read(b)) != -1) {

bos.write(b, 0, len);

}

bos.flush();

bis.close();

httpUrl.disconnect();

System.out.println("下载完毕。");

} catch (Exception e) {

System.out.println("无效下载地址,错误信息:" + e.getMessage());

this.msg = "下载失败!";

this.success = false;

} finally {

try {

if (bis != null)

bis.close();

if (bos != null)

bos.close();

} catch (IOException e) {

System.out.println("系统错误,错误信息:" + e.getMessage());

System.out.println("请联系管理员!");

}

}





// 下载到本地结束



// 取绝对路径+名

String filepath = apath + "\\" + filename1;





System.out.println(filepath);

// 转格式

DocConverter d = new DocConverter(filepath);

d.conver();

// 转格式结束

return "success";

}

转码须要用到两个软件!

Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN

swftools

必须先启动这两个软件,才干转码