java循环生成二维码存入压缩包(临时文件)
2018-01-03 09:26
609 查看
@RequestMapping(value = "/Download")
public void Download(HttpServletRequest request,HttpServletResponse response,String sequence,String QRType,String QRTypes) throws Exception {
try {
String userId = SessionUtil.getDataFromSession("JYHUSER", JyhUsers.class).getId();
String[] sequenceAll = null;
if(sequence!=null){
sequenceAll = sequence.split(",");
}else{
return;
}
//根据sequenceI拿到标贴信息
YncLabel yncLabel = jyhYncService.getYncLabelBySequence(sequenceAll[0], "", userId);
String downloadFilename = null;
if(QRType==null){
if(QRTypes=="1"||QRTypes.equals("1")){//只下载二维码
downloadFilename = yncLabel.getBandNumber()+"二维码.zip";//文件的名称
}else if(QRTypes=="2"||QRTypes.equals("2")){//带模板的二维码
downloadFilename = yncLabel.getBandNumber()+"带模板的二维码.zip";//文件的名称
}
}
if(QRTypes==null){
if(QRType=="1"||QRType.equals("1")){//只下载二维码
downloadFilename = yncLabel.getBandNumber()+"二维码.zip";//文件的名称
}else if(QRType=="2"||QRType.equals("2")){//带模板的二维码
downloadFilename = yncLabel.getBandNumber()+"带模板的二维码.zip";//文件的名称
}
}
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
File[] files =new File[sequenceAll.length];
for(int i=0;i<sequenceAll.length;i++){//创建临时文件
files[i]= File.createTempFile(sequenceAll[i].toString(), ".png");
}
String path = request.getSession().getServletContext().getRealPath("/")+ "/resources/images";
for(int i=0;i<sequenceAll.length;i++){
String html = wxConstant.OFFICIAL_WEBSITE+"/****.do?sequence="+sequenceAll[i].toString()+"&userId="+userId;
File f = null;
if(QRType==null){
if(QRTypes=="1"||QRTypes.equals("1")){//只下载二维码
QRCodeUtil.encode(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequence.jpg");
}else if(QRTypes=="2"||QRTypes.equals("2")){//带模板的二维码
QRCodeUtil.encode1(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequenceTemplate.jpg");
}
}
if(QRTypes==null){
if(QRType=="1"||QRType.equals("1")){//只下载二维码
QRCodeUtil.encode(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequence.jpg");
}else if(QRType=="2"||QRType.equals("2")){//带模板的二维码
QRCodeUtil.encode1(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequenceTemplate.jpg");
}
}
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
int r = 0;
FileOutputStream fo = new FileOutputStream(files[i]);
while ((r = fis.read(buffer)) != -1) {
fo.write(buffer, 0, r);
}
fo.close();
fis.close();
//下载的序列号,下载次数+1
jyhYncService.addDownloadNumber(sequenceAll[i].toString(),(yncLabel.getDownload()+1));
}
for (int i=0;i<files.length;i++) {
zos.putNextEntry(new ZipEntry(sequenceAll[i].toString()+".jpg"));
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
zos.flush();
zos.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 下载二维码的接口
*/
public static void encode(String sequence,String content, String imgPath, String destPath,boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);
mkdirs(destPath);
String file = "sequence.jpg";
Graphics g = image.getGraphics();//开启画图
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.PLAIN, 12));
g.drawString("S/N:"+sequence, 53, 270);
g.dispose();
ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
}
public void Download(HttpServletRequest request,HttpServletResponse response,String sequence,String QRType,String QRTypes) throws Exception {
try {
String userId = SessionUtil.getDataFromSession("JYHUSER", JyhUsers.class).getId();
String[] sequenceAll = null;
if(sequence!=null){
sequenceAll = sequence.split(",");
}else{
return;
}
//根据sequenceI拿到标贴信息
YncLabel yncLabel = jyhYncService.getYncLabelBySequence(sequenceAll[0], "", userId);
String downloadFilename = null;
if(QRType==null){
if(QRTypes=="1"||QRTypes.equals("1")){//只下载二维码
downloadFilename = yncLabel.getBandNumber()+"二维码.zip";//文件的名称
}else if(QRTypes=="2"||QRTypes.equals("2")){//带模板的二维码
downloadFilename = yncLabel.getBandNumber()+"带模板的二维码.zip";//文件的名称
}
}
if(QRTypes==null){
if(QRType=="1"||QRType.equals("1")){//只下载二维码
downloadFilename = yncLabel.getBandNumber()+"二维码.zip";//文件的名称
}else if(QRType=="2"||QRType.equals("2")){//带模板的二维码
downloadFilename = yncLabel.getBandNumber()+"带模板的二维码.zip";//文件的名称
}
}
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
File[] files =new File[sequenceAll.length];
for(int i=0;i<sequenceAll.length;i++){//创建临时文件
files[i]= File.createTempFile(sequenceAll[i].toString(), ".png");
}
String path = request.getSession().getServletContext().getRealPath("/")+ "/resources/images";
for(int i=0;i<sequenceAll.length;i++){
String html = wxConstant.OFFICIAL_WEBSITE+"/****.do?sequence="+sequenceAll[i].toString()+"&userId="+userId;
File f = null;
if(QRType==null){
if(QRTypes=="1"||QRTypes.equals("1")){//只下载二维码
QRCodeUtil.encode(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequence.jpg");
}else if(QRTypes=="2"||QRTypes.equals("2")){//带模板的二维码
QRCodeUtil.encode1(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequenceTemplate.jpg");
}
}
if(QRTypes==null){
if(QRType=="1"||QRType.equals("1")){//只下载二维码
QRCodeUtil.encode(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequence.jpg");
}else if(QRType=="2"||QRType.equals("2")){//带模板的二维码
QRCodeUtil.encode1(sequenceAll[i].toString(),html, "", path, false);
f= new File (path+"/sequenceTemplate.jpg");
}
}
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
int r = 0;
FileOutputStream fo = new FileOutputStream(files[i]);
while ((r = fis.read(buffer)) != -1) {
fo.write(buffer, 0, r);
}
fo.close();
fis.close();
//下载的序列号,下载次数+1
jyhYncService.addDownloadNumber(sequenceAll[i].toString(),(yncLabel.getDownload()+1));
}
for (int i=0;i<files.length;i++) {
zos.putNextEntry(new ZipEntry(sequenceAll[i].toString()+".jpg"));
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
zos.flush();
zos.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 下载二维码的接口
*/
public static void encode(String sequence,String content, String imgPath, String destPath,boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);
mkdirs(destPath);
String file = "sequence.jpg";
Graphics g = image.getGraphics();//开启画图
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.PLAIN, 12));
g.drawString("S/N:"+sequence, 53, 270);
g.dispose();
ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
}
相关文章推荐
- java打包文件生成zip压缩包
- java静态页面生成技术--循环输出列表--BUG临时修复
- java读取文件以字节流的形式读取 然后转化为字节流数组,最后经过SHA1加密生成返回16进制字符串存入数据库
- 使用PHP生成二维码带logo图像(不生成临时图片文件)
- Java Web 生成临时文件并下载(原)
- java生成二维码到文件,java生成二维码转成BASE64
- 【JAVA】纯JSP文件二维码生成代码分享
- java.io.File.deleteOnExit()-生成临时文件,删除临时文件
- 【JAVA】纯JSP文件二维码生成代码分享
- java.io.File.deleteOnExit()-生成临时文件,删除临时文件
- Laszal Mail 授权文件的生成 -> Java 帮助类技术实例
- java映射oracle生成一些映射文件
- java应用程序中动态生成PDF文件
- 生成一个java编制的可执行文件
- 如何将JAVA生成可执行文件?
- 应用middlegen同时生成.java和.hbm.xml文件
- 生成PDF文件的Java库iText
- 利用Eclipse和JSmooth生成java EXE可执行文件
- 生成一个java编制的可执行文件
- 利用Eclipse和JSmooth生成java EXE可执行文件