现在的位置: 首页 > 综合 > 正文

图片处理

2013年12月09日 ⁄ 综合 ⁄ 共 14663字 ⁄ 字号 评论关闭

1.      
把图片写入硬盘

// 获取线路code

                  code =
util
.getCircuitryCode(orgCode, po.getStartplace(),po.getTargetplace(), String.valueOf(po.getTraveldays()));

 

                  //
工程类路径,用来获取保存头像的路径

                  String url = request.getSession().getServletContext()

                         .getRealPath(request.getRequestURI());

                  int a = url.lastIndexOf(File.separator);

                  url = url.substring(0, a);

                  StringBuffer dirPath =
new StringBuffer(url).append(

                         File.separator).append("picture");

 

                  filePath = fileItem.getName();

                  if (filePath !=
null && !"".equalsIgnoreCase(filePath)) {

                     //
创建文件夹

                     createdir[微软用户1] (code,
dirPath);

                     TAttachment ta =
new TAttachment();

                     //
获取图片或文件的名字

                     fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);

 

                     //
循环图片名字判断是否有重复的

                     for (int i = 0; i < pathnameList.size();
i++) {

                         String beforFileName = (String) pathnameList.get(i);

                         if (beforFileName.equals(fileName)) {

                           fileName =
"little"+ i+ filePath.substring(filePath.lastIndexOf("\\") + 1);

                         }

                     }

                     dirPath.append("/");

                     outPath = dirPath.toString() + fileName;

                     //
写入文件

                     if (!"".equals(filePath)) {

                         ins = fileItem.getInputStream();

                         os =
new
FileOutputStream(outPath);

                         bytesRead = 0;

                         buffer =
newbyte[4096];

                         while ((bytesRead = ins.read(buffer, 0, 4096)) != -1) {

                            os.write(buffer, 0, bytesRead);

                         }

                         buffer =
null;

                         os.close();

                         ins.close();

                     }

                     fileSize = fileItem.getSize();

                     //
把图片名字添加到list中,以便判断是否有相同的图片名字

                     pathnameList.add(fileName);

                     extend1 = fileName.substring(fileName.lastIndexOf(".") + 1);

                     byte[] bytes =
newbyte[12];

                     ta.setName(fileName);

                     ta.setContexts(bytes);

                     ta.setCreatetime(new Date());

                     ta.setCreatorid(creatorid);

                     ta.setCreatororgid(creatororgid);

                     ta.setCreatorip(ip);

                     ta.setExtend(extend1);

                     ta.setFilepath("/picture/circuitryPictrue/" + code

                            +
"/"
+ fileName);

 

 

 

 

 

 

 

 

 

 

 

 

2.    复制

code = util.getCircuitryCode(orgCode, po.getStartplace(),po.getTargetplace(), String.valueOf(po.getTraveldays()));

                  //
工程类路径,用来获取保存头像的路径

                  String url = request.getSession().getServletContext().getRealPath(request.getRequestURI());

                  int a = url.lastIndexOf(File.separator);

                  url = url.substring(0, a);

                  StringBuffer dirPaths =
new StringBuffer(url);//
用来删除文件用的

                  StringBuffer dirPath =
new StringBuffer(url).append(File.separator).append("picture");

                  String fileid = fileItem.getFieldName();
// 用来获取浏览文本框的ID

                  filePath = fileItem.getName();

                  //
获取图片或文件的名字

                  fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);

                  if (fileid.equals("haveBigPic")) {

                     if (filePath ==
null || filePath.equals("")) {

                         type =
"BIGPIC"
;

                         attachement =
logic.getAttachment(relationId, type);

                         //
调用复制的方法

                         filePath =
copyPicture[微软用户2] (code,
ip, filePath,creatororgid, creatorid, set_ta, tattBig,attachement, dirPaths, dirPath);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3. 修改TAttachment ta =
null;

                  //
工程类路径,用来获取保存头像的路径

                  String url = request.getSession().getServletContext()

                         .getRealPath(request.getRequestURI());

                  int a = url.lastIndexOf(File.separator);

                  url = url.substring(0, a);

                  StringBuffer dirPath =
new StringBuffer(url).append(

                         File.separator).append("picture");

                 

                  StringBuffer dirPaths =
new StringBuffer(url);//
此字符串用来删除文件夹的

                  String fileid = fileItem.getFieldName();
// 用来获取浏览文本框的ID

                  filePath = fileItem.getName();

                  if (filePath !=
null && !"".equalsIgnoreCase(filePath)) {

                     //
创建文件夹

                     createdir(code, dirPath);

                     fileName = filePath.substring(filePath

                            .lastIndexOf("\\") + 1);

                     //
调用处理图片同名的方法

                     fileName =
picrename
[微软用户3] (code,
filePath, fileName, po,

                            pathnameList, dirPaths, fileid);

                     dirPath.append("/");

                     outPath = dirPath.toString() + fileName;

                     //
写入文件说

                     if (!"".equals(filePath)) {

                         ins = fileItem.getInputStream();

                         os =
new
FileOutputStream(outPath);

                         bytesRead = 0;

                         buffer =
newbyte[4096];

                         while ((bytesRead = ins.read(buffer, 0, 4096)) != -1) {

                            os.write(buffer, 0, bytesRead);

                         }

                         buffer =
null;

                         os.close();

                         ins.close();

                     }

                     fileSize = fileItem.getSize();

                     pathnameList.add(fileName);//
filename存入list,用来判断图片重名

                     extend1 = fileName

                            .substring(fileName.lastIndexOf(".") + 1);

                     byte[] bytes =
newbyte[12];

                     Set ts = po.getTAttachments();

                     Iterator it = ts.iterator();

                     boolean flag =
false;

                     if ("haveBigPic".equalsIgnoreCase(fileItem

                            .getFieldName())) {

                         while (it.hasNext()) {

                            ta = (TAttachment) it.next();

                            if (Constants.C_ATTACHMENT_TYPE_BIG

                                  
.equalsIgnoreCase(ta.getType())) {

                               
flag = true;

                               
break;

                            }

                         }

                     } elseif ("haveLittlePic".equalsIgnoreCase(fileItem

                            .getFieldName())) {

                         while (it.hasNext()) {

                            ta = (TAttachment) it.next();

                            if (Constants.C_ATTACHMENT_TYPE_SMALL.equalsIgnoreCase(ta.getType()))
{

                                flag =
true;

                               
break;

                            }

                         }

                     } elseif ("haveWordFile".equalsIgnoreCase(fileItem

                            .getFieldName())) {

                         while (it.hasNext()) {

                            ta = (TAttachment) it.next();

                            if (Constants.C_ATTACHMENT_TYPE_DOC

                                    .equalsIgnoreCase(ta.getType())) {

                               
flag = true;

                               
break;

                            }

                         }

                     }

                     if (flag) {

                         if (Constants.C_ATTACHMENT_TYPE_BIG

                               
.equalsIgnoreCase(ta.getType())) {

                            if ("bmp".equalsIgnoreCase(extend1)

                                   ||
"gif".equalsIgnoreCase(extend1)

                                  
|| "jpg".equalsIgnoreCase(extend1)

                                  
|| "jpeg".equalsIgnoreCase(extend1)

                                  
|| "tiff".equalsIgnoreCase(extend1)

                                  
|| "psd".equalsIgnoreCase(extend1)

                                  
|| "png".equalsIgnoreCase(extend1)

                                  
|| "swf".equalsIgnoreCase(extend1)) {

                               
ta.setKind(Constants.C_ATTACHMENT_KIND_PHO);

                               
ta.setType(Constants.C_ATTACHMENT_TYPE_BIG);

                            }
else
{

                               
thrownew BusinessException("请上传正确的格式图片");

                            }

                         }
elseif
(Constants.C_ATTACHMENT_TYPE_SMALL

                               
.equalsIgnoreCase(ta.getType())) {

                            if ("jpg".equalsIgnoreCase(extend1))
{

                               
ta.setKind(Constants.C_ATTACHMENT_KIND_PHO);

                               
ta

                                      
.setType(Constants.C_ATTACHMENT_TYPE_SMALL);

                            }
else
{

                               
thrownew BusinessException("请上传JPG格式图片");

                            }

                         }
elseif
(Constants.C_ATTACHMENT_TYPE_DOC

                               
.equalsIgnoreCase(ta.getType())) {

                            if ("doc".equalsIgnoreCase(extend1)

                                   ||
"pdf".equalsIgnoreCase(extend1)

                                  
|| "xls".equalsIgnoreCase(extend1)) {

                               
ta.setKind(Constants.C_ATTACHMENT_KIND_DOC);

                               
ta.setType(Constants.C_ATTACHMENT_TYPE_DOC);

                            }
else
{

                               
thrownew BusinessException(

                                      
"请上传WORD文档或PDF文件");

                            }

                         }

                         ta.setContexts(bytes);

                         ta.setName(fileName);

                         ta.setContexts(bytes);

                         ta.setCreatetime(new Date());

                         ta.setCreatorid(changerid);

                         ta.setCreatororgid(changerorgid);

                         ta.setCreatorip(ip);

                         ta.setExtend(extend1);

                         ta.setFilepath("/picture/circuitryPictrue/" + code

                               
+ "/" + fileName);

                         ta.setFilesize(String.valueOf(fileSize));

                         set.add(ta);

 

 

 

3.    读取到界面

    //
把图片读入页面

    public ActionForward readpic(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           throws Exception {

       File uploadFile =
null
;

       try {

           String id = request.getParameter("id");

           TAttachment t = (TAttachment)
logic.getDao().getHibernateTemplate()

                  .get(TAttachment.class, id);

           //
获取取数据库中的filepath路径

           String bytes = t.getFilepath();

           //
工程类路径,用来获取保存头像的路径

           String url = request.getSession().getServletContext().getRealPath(

                  request.getRequestURI());

           int a = url.lastIndexOf(File.separator);

           url = url.substring(0, a);

           BufferedInputStream bis =
null;

           BufferedOutputStream bos =
null;

           OutputStream fos =
null
;

           InputStream fis =
null
;

           String filepath = bytes;

           uploadFile =
new
File(url + filepath);

           fis = new FileInputStream(uploadFile);

           bis = new BufferedInputStream(fis);

           fos = response.getOutputStream();

           bos = new BufferedOutputStream(fos);

           //
这个就就是弹出下载对话框的关键代码

           // response.setHeader("Content-disposition", "attachment;filename="

           // + URLEncoder.encode(bytes, "utf-8"));

           int bytesRead = 0;

           //
用输入流进行先读,然后用输出流去写

           byte[] buffer =
newbyte[8192];

           while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {

              bos.write(buffer, 0, bytesRead);

           }

           bos.flush();

           fis.close();

           bis.close();

           fos.close();

           bos.close();

       } catch (Exception ex) {

           return mapping.findForward("error");

       }

       returnnull;

4.       }

 

 

 

5下载附件,如wordpdf等等

public ActionForward download(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           throws Exception {

       byte[] buffer;

       InputStream ins;

       String id =
""
, dirPaths, filepath;

       try {

           //
工程类路径,用来获取保存头像的路径

           String url = request.getSession().getServletContext().getRealPath(

                  request.getRequestURI());

           int a = url.lastIndexOf(File.separator);

           url = url.substring(0, a);

 

           id = request.getParameter("id");

           TAttachment ta = (TAttachment)
logic.load(TAttachment.class, id);

           filepath = ta.getFilepath();

           dirPaths = url + filepath;

 

           //
读取文件

           if (!"".equals(dirPaths)) {

              ins = new FileInputStream(dirPaths);

              int bytesRead = 0;

              buffer = newbyte[4096];

              response.setContentType("application/x-msdownload");

              response.setHeader("Content-Disposition",
"attachment;"

                     +
" filename="

                     + new String(ta.getName().getBytes(),
"ISO-8859-1"));

              while ((bytesRead = ins.read(buffer, 0, 4096)) != -1) {

                  logic.write(response.getOutputStream(), buffer);

              }

              buffer = null;

              ins.close();

           }

       } catch (Exception ex) {

           request.setAttribute(Constants.C_ERROR_MSG,
"文件下载出现错误");

           return mapping.findForward("error");

       }

       returnnull;

    }

 

 

 

 

 

 

 

6Byte转换为输入流

// 获取没有存入到硬盘的图片,然后把图片添加到硬盘

    public ActionForward movePitureToDir(ActionMapping mapping,

           ActionForm form, HttpServletRequest request,

           HttpServletResponse response)
throws Exception {

       String outPath =
""
, fileName;

       byte[] buffer =
null;

       OutputStream os =
null
;

       byte[] bytes =
newbyte[12];

       byte[] bytes1;

       Map map = new HashMap();

       //
工程类路径,用来获取保存图片的路径

       String url = request.getSession().getServletContext().getRealPath(request.getRequestURI());

       int a = url.lastIndexOf(File.separator);

       url = url.substring(0, a);

       StringBuffer dirPath =
new
StringBuffer(url).append(File.separator).append("picture");

 

       //
查找附件信息

       List list =
logic
.getAttamentsByfilePath("/picture/circuitryPictrue");

       if (list !=
null) {

           //
创建文件夹picture

           File fpath =
new
File(dirPath.toString());

           if (!fpath.exists()) {

              fpath.mkdir();

           }

           //
创建用户照片文件夹

           dirPath.append("/").append("circuitryPictrue");

           fpath = new File(dirPath.toString());

           if (!fpath.exists()) {

              fpath.mkdir();

           }

           dirPath.append("/");

           for (int i = 0; i < list.size(); i++)
{

              map = (Map) list.get(i);

              String id = map.get("id").toString();

              TAttachment attaches =
logic.getAttachment(id);

              TCircuitry tc = (TCircuitry)
logic.getDao().getHibernateTemplate().get(TCircuitry.class,attaches.getRelationid());

              if (tc !=
null) {

                  StringBuffer strbf =
new StringBuffer(dirPath);

                  //
创建某条线路的图片文件夹

                  strbf.append(tc.getCode());

                  fpath =
new
File(strbf.toString());

                  //
如果用户文件夹不存在,则创建用户文件夹

               

抱歉!评论已关闭.