package com.pdf.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.pdf.util.ImagesPDFUtil;
/**
* ͼƬת����pdf
* @author chenguoji
*
*/
@Controller
public class ImagesAndPDF {
/**
* �ϴ��ļ�������ת��
* @param request
* @param response
* @return
*/
@SuppressWarnings("unchecked")
@RequestMapping("/images")
public String images(HttpServletRequest request,HttpServletResponse response){
//�õ��ϴ��ļ��ı���Ŀ¼
String savePath = request.getServletContext().getRealPath("/upload");
File file = new File(savePath);
//�ж��ϴ��ļ��ı���Ŀ¼�Ƿ����
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath+"Ŀ¼�����ڣ���Ҫ����");
//����Ŀ¼
file.mkdir();
}
//����PDF�ļ�Ŀ¼
String pdfUrl = request.getServletContext().getRealPath("/pdf");
File file2 = new File(pdfUrl);
//�ж��ϴ��ļ��ı���Ŀ¼�Ƿ����
if (!file2.exists() && !file2.isDirectory()) {
System.out.println(pdfUrl+"Ŀ¼�����ڣ���Ҫ����");
//����Ŀ¼
file2.mkdir();
}
//��Ϣ��ʾ
String message = "";
try{
//ʹ��Apache�ļ��ϴ���������ļ��ϴ����裺
//1������һ��DiskFileItemFactory����
DiskFileItemFactory factory = new DiskFileItemFactory();
//2������һ���ļ��ϴ�������
ServletFileUpload upload = new ServletFileUpload(factory);
//����ϴ��ļ������������
upload.setHeaderEncoding("UTF-8");
//3���ж��ύ����������Ƿ����ϴ��?�����
if(!ServletFileUpload.isMultipartContent(request)){
//���մ�ͳ��ʽ��ȡ���
return "error";
}
//4��ʹ��ServletFileUpload�����������ϴ���ݣ��������ص���һ��List<FileItem>���ϣ�ÿһ��FileItem��Ӧһ��Form�?��������
List<FileItem> list = upload.parseRequest(request);
for(FileItem item : list){
//���fileitem�з�װ������ͨ����������
if(item.isFormField()){
String name = item.getFieldName();
//�����ͨ���������ݵ�������������
String value = item.getString("UTF-8");
// value = new String(value.getBytes("iso8859-1"),"UTF-8");
System.out.println(name + "=" + value);
}else{//���fileitem�з�װ�����ϴ��ļ�
//�õ��ϴ����ļ���ƣ�
String filename = item.getName();
if(filename==null || filename.trim().equals("")){
continue;
}
//ע�⣺��ͬ��������ύ���ļ����Dz�һ��ģ���Щ������ύ�������ļ����Ǵ���·���ģ��磺 c:\a\b\1.txt������Щֻ�ǵ������ļ����磺1.txt
//�����ȡ�����ϴ��ļ����ļ����·�����֣�ֻ�����ļ����
filename = filename.substring(filename.lastIndexOf("\\")+1);
//��ȡitem�е��ϴ��ļ���������
InputStream in = item.getInputStream();
//����һ���ļ������
FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
//����һ��������
byte buffer[] = new byte[1024];
//�ж��������е�����Ƿ��Ѿ�����ı�ʶ
int len = 0;
//ѭ�������������뵽�������У�(len=in.read(buffer))>0�ͱ�ʾin���滹�����
while((len=in.read(buffer))>0){
//ʹ��FileOutputStream�����������������д�뵽ָ����Ŀ¼(savePath + "\\" + filename)����
out.write(buffer, 0, len);
}
//�ر�������
in.close();
//�ر������
out.close();
//ɾ�����ļ��ϴ�ʱ��ɵ���ʱ�ļ�
item.delete();
message = "�ļ��ϴ��ɹ���";
System.out.println("ͼƬ�ϴ�·����"+savePath);
System.out.println("ͼƬ�ϴ�·���ļ���ƣ�"+filename);
System.out.println("ͼƬ�ϴ�·���ļ���ƺ���"+item.getContentType());
//ת����pdf�ļ�
String imageUrl = savePath + "\\" + filename;
String name = filename.substring(0,filename.lastIndexOf("."));//��ȡ���1λ�����
//getFilename .substring(getFilename .lastIndexOf("."));//����ǻ�ȡ����
new ImagesPDFUtil().jpg2Pdf(imageUrl, pdfUrl+"\\" + name+".pdf");
}
}
}catch (Exception e) {
message= "�ļ��ϴ�ʧ�ܣ�";
e.printStackTrace();
}
request.setAttribute("success", message);
return "redirect:/query";
}
/**
* ��ѯת����pdf�ļ�
* @param request
* @param response
* @return
*/
@RequestMapping("/query")
public String query(HttpServletRequest request, HttpServletResponse response){
//ת��Ŀ¼
String pdfPath = request.getServletContext().getRealPath("/pdf");
Map<String,String> pdfMap = new HashMap<String,String>();
listfile(new File(pdfPath),pdfMap);//File�ȿ��Դ��һ���ļ�Ҳ���Դ��һ��Ŀ¼
request.setAttribute("pdfMap", pdfMap);
return "message";
}
/**
* @Description: �ݹ����ָ��Ŀ¼�µ������ļ�
* @param file �����һ���ļ���Ҳ���һ���ļ�Ŀ¼
* @param map �洢�ļ����Map����
*/
public void listfile(File file,Map<String,String> map){
//���file���IJ���һ���ļ�������һ��Ŀ¼
if(!file.isFile()){
//�г���Ŀ¼�µ������ļ���Ŀ¼
File files[] = file.listFiles();
//����files[]����
for(File f : files){
//�ݹ�
listfile(f,map);
}
}else{
/**
* �������
*/
String realName = file.getName().substring(file.getName().indexOf("_")+1);
//file.getName()�õ������ļ���ԭʼ��ƣ���������Ψһ�ģ���˿�����Ϊkey��realName�Ǵ��������ƣ��п��ܻ��ظ�
map.put(file.getName(), realName);
}
}
/**
* �����ļ�
* @param path pdf·��
* @param name ����
* @param request
* @param response
*/
@RequestMapping("/download")
public void download(String filename, HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
//�õ�Ҫ���ص��ļ���
String fileName = filename;
fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
//ת����pdf�ļ�������pdfĿ¼����
String path=request.getServletContext().getRealPath("/pdf");
//�õ�Ҫ���ص��ļ�
File file = new File(path + "\\" + fileName);
//����������
if(!file.exists()){
request.setAttribute("message", "��Ҫ���ص���Դ�ѱ�ɾ��");
return;
}
//�������
String realname = fileName.substring(fileName.indexOf("_")+1);
//������Ӧͷ��������������ظ��ļ�
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
//��ȡҪ���ص��ļ������浽�ļ�������
FileInputStream in = new FileInputStream(path + "\\" + fileName);
//���������
OutputStream out = response.getOutputStream();
//����������
byte buffer[] = new byte[1024];
int len = 0;
//ѭ�����������е����ݶ�ȡ����������
while((len=in.read(buffer))>0){
//�������������ݵ��������ʵ���ļ�����
out.write(buffer, 0, len);
}
//�ر��ļ�������
in.close();
//�ر������
out.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最近下载更多
Sisphyus LV1
2022年2月17日
我的最代码 LV20
2022年1月14日
xuexizhuanyong23 LV16
2021年9月24日
xmjying LV13
2020年3月19日
lsongssl LV2
2019年6月14日
DU-ZB LV13
2019年3月27日
麻辣小龙虾 LV11
2019年3月12日
zhangshu6682 LV7
2019年1月6日
tanyunqing LV8
2018年7月11日
gggbbbb LV1
2018年6月12日

最近浏览