package bootstarter;
import weaver.mvc.annotations.Controller;
import weaver.mvc.annotations.RequestMapping;
import weaver.mvc.mode.HttpMode;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
// /mvc/accessory/getPictureByid?picID=241164
@Controller(URL = "accessory")
public class GetAccessory2 extends HttpServlet {
@RequestMapping(URL = "getPictureByid")
public void downloadPic(HttpMode mode, HttpServletResponse response){
BufferedInputStream is = null;
BufferedOutputStream os = null;
// weaver.conn.RecordSet rs = new weaver.conn.RecordSet();
String filename = "";// 文件名
String filepath = "";// 文件路径
try {
String id = mode.getValue("picID"); //替换回来
String picID = "241164";
System.out.println("picID:" + id);
// String fileSql = "select IMAGEFILENAME,FILEREALPATH from IMAGEFILE where IMAGEFILENAME like (select concat(docsubject,'%') from docdetail where id ="
// + picID + ") and IMAGEFILETYPE LIKE 'image%' and rownum = 1";
// rs.executeSql(fileSql);
// if (rs.next()) {
// filename = weaver.general.Util.null2String(rs.getString("imagefilename"));
// filepath = weaver.general.Util.null2String(rs.getString("filerealpath")); //替换回来
// }
filename = "登入成功.png";
filepath = "/weaver/ecology/filesystem/202208/Y/ad46e717-e18f-46e9-acc5-04803c8c5f80.zip"; //替换回来
String savepath = "";
int dex = filepath.lastIndexOf("."); // 截取最后的一个“/”符号,获取长度索引
if (dex > -1) {
savepath = filepath.substring(0, dex) + "."; // 获取到拼接的保存路径
}
int dex2 = filename.lastIndexOf(".")+1;
String suffix = filename.substring(dex2, filename.length());
filename = savepath + suffix;
System.out.println("filename:" + filename);
File file = new File(filename); // 读取到文件
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(file.getName()));
response.setHeader("Content-Length", String.valueOf(file.length()));
is = new BufferedInputStream(new FileInputStream(file));
os = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = is.read(buff, 0, buff.length))) {
os.write(buff, 0, bytesRead);
}
if (is != null)
is.close();
if (os != null)
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
实现图片下载
最新推荐文章于 2024-09-09 14:40:58 发布