团队冲刺第二阶段9

根据生成文件名读取照片并将照片分类到对应的文件夹中

对应代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package servlet;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FileUtils;
 
import com.baidu.ai.aip.auth.AdvancedGeneral;
import com.baidu.ai.aip.auth.text;
 
/**
 * Servlet implementation class photoservlet
 */
@WebServlet("/photoservlet")
public class photoservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public photoservlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String libdir = "D:\eclipse\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\picbook\upload";
        //String libdir = request.getSession().getServletContext().getRealPath("");
       //   File f= new File(libdir);
        //File f= new File("C:\Users\86188\Desktop\images");
        File f= new File(libdir);
        File file[] = f.listFiles();
        for(int i=0;i<file.length;i++) {
            if(file[i].isFile()) {
            AdvancedGeneral.advancedGeneral(file[i].toString());
            String a=AdvancedGeneral.getresult();
            out.print(AdvancedGeneral.getresult()+"<br>");
             
        //  PrintStream print=new PrintStream(new FileOutputStream(new File("C:\Users\86188\Desktop\图片识别.txt"),true));
            //System.setOut(print); //打印文本 
             
            if(a.toString().contains("人物")){
                File files = new File("D:\eclipse\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\picbook\upload\2-人(云端智能相册)\人"+i+".jpg");           
                copyFileUsingApacheCommonsIO(file[i], files);
            }
            else if(a.toString().contains("动物")) {
                 
                File files = new File("D:\eclipse\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\picbook\upload\4-动物(云端智能相册)\动物"+i+".jpg");
                copyFileUsingApacheCommonsIO(file[i], files);
            }
            else if(a.toString().contains("植物")) {
                File files = new File("D:\eclipse\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\picbook\upload\3-植物(云端智能相册)\植物"+i+".jpg");
                copyFileUsingApacheCommonsIO(file[i], files);
            }
            else if(a.toString().contains("商品")) {
                File files = new File("D:\eclipse\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\picbook\upload\5-商品(云端智能相册)\商品"+i+".jpg");
                copyFileUsingApacheCommonsIO(file[i], files);
            }
            else if(a.toString().contains("非自然")) {
                File files = new File("D:\eclipse\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\picbook\upload\1-非自然(云端智能相册)\非自然"+i+".jpg");
                copyFileUsingApacheCommonsIO(file[i], files);
            }
            }
             
             
             
        
         
         
    }
    private static void copyFileUsingApacheCommonsIO(File source, File dest)
            throws IOException {
          FileUtils.copyFile(source, dest);
          System.out.print("chenggong");
        }
     
    /*@SuppressWarnings("resource")
    private static void copyFileUsingFileChannels(File source, File dest) throws IOException {  
        FileChannel inputChannel = null;  
        FileChannel outputChannel = null;  
      try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
      } finally {
        inputChannel.close();
        outputChannel.close();
      }
    }*/
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
 
}
原文地址:https://www.cnblogs.com/1329197745a/p/14905688.html