当前位置: 代码迷 >> 综合 >> Jersey 409
  详细解决方案

Jersey 409

热度:62   发布时间:2024-01-12 17:48:14.0

Jersey 报409错误
解决方案 :
在webapp下新建upload文件夹 然后建一个file空文件
像这样:
在这里插入图片描述

Controller层:

package com.xiepanpan.ecps.controller;import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.xiepanpan.ecps.utils.ECPSUtils;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;/*** describe:图片上传** @author xiepanpan* @date 2018/10/31*/
@Controller
@RequestMapping("/upload")
public class EbUploadController {
    @RequestMapping("/uploadPic.do")public void uploadPic(HttpServletRequest request, PrintWriter printWriter) throws IOException {
    MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;//从表单中获取文件Iterator<String> iter = mr.getFileNames();String inputName = iter.next();// 获取文件MultipartFile multipartFile = mr.getFile(inputName);byte[] bytes = multipartFile.getBytes();//定义上传文件的文件名String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());Random random = new Random();//文件名后三位为随机数for (int i=0;i<3;i++){
    fileName=fileName+random.nextInt(10);}//获得后缀名String originalFilename = multipartFile.getOriginalFilename();String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));//相对路径保存到数据库中 绝对路径显示缩略图//要上传的绝对路径String relativePath="/upload/" + fileName + suffix;String realPath = ECPSUtils.readProp("upload_file_path") + relativePath;//创建Jersey客户端Client client = Client.create();//指定上传的绝对路径WebResource webResource = client.resource(realPath);webResource.put(bytes);JSONObject jsonObject = new JSONObject();jsonObject.accumulate("realPath",realPath);jsonObject.accumulate("relativePath",relativePath);String result = jsonObject.toString();printWriter.write(result);}}

ECPSUtil工具类:

package com.xiepanpan.ecps.utils;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;/*** describe:** @author xiepanpan* @date 2018/11/01*/
public class ECPSUtils {
    /*** 读取配置文件* @param key* @return*/public static String readProp(String key) {
    InputStream inputStream = ECPSUtils.class.getClassLoader().getResourceAsStream("ecps_prop.properties");Properties properties = new Properties();String value= null;try {
    properties.load(inputStream);value= properties.getProperty(key);} catch (IOException e) {
    e.printStackTrace();}return value;}
}

配置文件:

upload_file_path=http://localhost:8092/ecps-file

参考博客:
https://blog.csdn.net/saytime/article/details/51416017 这个博客写的更好 介绍的Jersey用法