chenyongxing 7 سال پیش
والد
کامیت
bc96d4f971
4فایلهای تغییر یافته به همراه90 افزوده شده و 52 حذف شده
  1. 3 1
      editor.iml
  2. 15 2
      pom.xml
  3. 8 49
      src/main/java/com/yihu/editor/controller/ArticleController.java
  4. 64 0
      src/main/java/com/yihu/editor/util/Upload.java

+ 3 - 1
editor.iml

@ -93,6 +93,8 @@
    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.0.30" level="project" />
    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.0.30" level="project" />
    <orderEntry type="library" name="Maven: org.eclipse.jdt.core.compiler:ecj:4.4.2" level="project" />
    <orderEntry type="library" name="Maven: repository.org.apache.httpcomponents:httpclient:4.3.4" level="project" />
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.4" level="project" />
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.6" level="project" />
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.5.3" level="project" />
  </component>
</module>

+ 15 - 2
pom.xml

@ -99,10 +99,23 @@
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>compile</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
		<dependency>
			<groupId>repository.org.apache.httpcomponents</groupId>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.4</version>
			<version>4.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.6</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.5.3</version>
		</dependency>

+ 8 - 49
src/main/java/com/yihu/editor/controller/ArticleController.java

@ -5,7 +5,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.editor.entity.SystemDictItem;
import com.yihu.editor.entity.YdfHealthEduArticle;
import com.yihu.editor.service.ArticleService;
import com.yihu.editor.util.Upload;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
@ -95,56 +97,13 @@ public class ArticleController extends BaseController{
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(@ModelAttribute @Valid YdfHealthEduArticle article,HttpServletRequest request,MultipartHttpServletRequest multiReq){
        List<MultipartFile> files = multiReq
                .getFiles("file");
        List<MultipartFile> files = multiReq.getFiles("file");
        //---------------------------------------cyx
        String url="http://172.19.103.31:10001/ydf17-6-10/upload/image?callback=call";
        /*HttpClient httpClient = null;
        HttpPost httpPost;
        String result = null;
        try {
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(url);
            String[] filenames=fileNames.split(";");
            MultipartEntity reqEntity = new MultipartEntity();
            for(int i=0;i<filenames.length;i++) {
                String fileName=filenames[i];
                FileBody file = new FileBody(new File(fileName));
                reqEntity.addPart("file"+i, file);// file1为请求后台的File upload;属性
            }
            httpPost.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (null != response && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                if (null != resEntity) {
                    result = EntityUtils.toString(resEntity, HTTP.UTF_8);
                    System.out.println(result);
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            httpClient.getConnectionManager().shutdown();
        }
        return result;
    }*/
        //------------------------------------------
        String url="http://172.19.103.31:10001/ydf/upload/image";
        String result = Upload.httpClientUploadFile(url,files);
        JSONObject resultJson = new JSONObject(result);
        String fileUrls = (String) resultJson.get("urls");
        article.setImages(fileUrls);
        if(StringUtils.isBlank(article.getId())){
            article.setId(UUID.randomUUID().toString().replaceAll("-",""));
        }

+ 64 - 0
src/main/java/com/yihu/editor/util/Upload.java

@ -0,0 +1,64 @@
package com.yihu.editor.util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
 * Created by Administrator on 2017/6/13 0013.
 */
public class Upload {
    /**
     * 中转文件
     *
     * @param files
     *            上传的文件
     * @return 响应结果
     */
    public static String httpClientUploadFile(String url,List<MultipartFile> files) {
        final String remote_url = url;// 第三方服务器请求地址
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(remote_url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            int i=0;
            for(MultipartFile file:files){
                String fileName = file.getOriginalFilename();
                builder.addBinaryBody("file"+i, file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
                builder.addTextBody("filename"+i, fileName);// 类似浏览器表单提交,对应input的name和value
                i++;
            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);// 执行提交
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                // 将响应内容转换为字符串
                result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}