空_灵 2015-09-03 02:21 采纳率: 0%
浏览 1466
已结题

为什么我客户端传图片到服务器不成功啊 求大神解决问题啊

客户端代码:
public void run() {
String boundry = UUID.randomUUID().toString(); // 边界标识
String prefix = "--", end = "\r\n";
File file = new File(imagePath);

    try {
        URL Url = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) Url.openConnection();
        conn.setReadTimeout(5000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-type", "multipart/form-data;boundary=" + boundry);
        if (file != null) {
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.writeBytes(prefix + boundry + end);
            out.writeBytes("Content-Disposition: form-data;" + "name=\"file\"; filename=\"" + file.getName()
                    + "\"" + end);
            out.writeBytes(end);
            FileInputStream fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = fis.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.writeBytes(end);
            out.writeBytes(prefix + boundry + prefix + end);
            out.flush();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String str = "";
            while ((str = reader.readLine()) != null) {
                sb.append(str);
            }
            if (out != null) {
                out.close();
            }
            if (reader != null) {
                reader.close();
            }
        } 
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

服务器代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    request.setCharacterEncoding("utf-8");
    DiskFileItemFactory factory = new DiskFileItemFactory();
    String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");

    factory.setRepository(new File(path));
    factory.setSizeThreshold(1024 * 1024);
    ServletFileUpload upload = new ServletFileUpload(factory);

    try {
        List<FileItem> list = (List<FileItem>) upload.parseRequest(request);
        for (FileItem item : list) {
            String name = item.getFieldName();
            if (item.isFormField()) {
                String value = item.getString();
                request.setAttribute(name, value);
            } else {
                String value = item.getName();
                int start = value.lastIndexOf("\\");
                String filename = value.substring(start + 1);
                request.setAttribute(name, filename);
                try {
                    item.write(new File(path, filename));
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

  • 写回答

1条回答 默认 最新

  • devmiao 2015-09-03 15:26
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并