两江总督首席码农 2015-10-20 12:50 采纳率: 0%
浏览 3380
已结题

图片上传返回JSON数据,前台弹出下载保存文件框, 文件里面是JSON数据,页面上没显示图片怎么解决

@RequestMapping(value = "kinduploadfile", method = RequestMethod.POST)
public @ResponseBody
JSONObject KindUploadFile(HttpServletRequest request,HttpServletResponse response)
throws FileUploadException {
response.setContentType("text/html;charset=UTF-8");
JSONObject obj = new JSONObject();
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = (cal.get(Calendar.MONTH)) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
Map map = new HashMap();
String rootPath = "/upload/";
rootPath += year + "/" + month + "/" + day + "/";
String realRootPath = request.getServletContext().getRealPath(rootPath);
java.io.File f = new File(realRootPath);
if (f.exists() == false) {
f.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
.getFile("imgFile");// 这里是表单的名字,在swfupload.js中this.ensureDefault("file_post_name",
// "filedata");
try {

        InputStream stream = file.getInputStream();
        String oldfileName = file.getOriginalFilename();
        String type = oldfileName.split("\\.")[1];// 获取文件类型
        oldfileName = new String(oldfileName.getBytes(), "utf-8");
        String filename = rootPath + sdf.format(new Date())
                + (int) ((Math.random() + 1) * 100000) + "." + type;
        OutputStream bos = new FileOutputStream(request.getServletContext()
                .getRealPath(filename));
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }

        bos.close();
        // close the stream
        stream.close();

        obj.put("url", filename);
        obj.put("error",0);

    } catch (Exception e) {

        obj.put("error", "1");
        obj.put("message", e.getMessage());
    }
    // 上传操作

    return obj;

}
  • 写回答

1条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 android百度地图SDK海量点显示标题
    • ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错
    • ¥15 这段代码可以正常运行,打包后无法执行,在执行for内容之前一直不断弹窗,请修改调整
    • ¥15 C语言判断有向图是否存在环路
    • ¥15 请问4.11到4.18以及4.27和4.29公式的具体推导过程是怎样的呢
    • ¥20 将resnet50中的卷积替换微ODConv动态卷积
    • ¥15 通过文本框输入商品信息点击按钮将商品信息列举出来点击加入购物车商品信息添加到表单中
    • ¥100 这是什么压缩算法?如何解压?
    • ¥20 upload上传实验报错500,如何解决?(操作系统-windows)
    • ¥15 谁知道 ShaderGraph 那个节点可以接入 Particle System -> Custom Data