BullWark 2017-07-18 05:06 采纳率: 100%
浏览 1886
已采纳

表单文件域上传 HTTP Status500 服务器遇到了一个内部错误()阻止了它执行此请求

 package com.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.io.FileUtils;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
public class FileUploadController {


    @RequestMapping("/fileupload")
public String shangchuan(
        @RequestParam(value = "file", required = false) CommonsMultipartFile file,
        HttpServletRequest request, ModelMap map) {
    String path = request.getRealPath("upload");
    String fileName = file.getOriginalFilename();
    System.out.println(path + "/" + fileName);
    File f = new File(path, fileName);

    try {
        file.transferTo(f);
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    request.setAttribute("picUrl", "upload/" + fileName);
    return "upload.jsp";

}

public static void downLoadFile(HttpServletResponse response, File file) {
    if (file == null || !file.exists()) {
        return;
    }
    OutputStream out = null;
    try {
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename="
                + file.getName());
        out = response.getOutputStream();
        out.write(FileUtils.readFileToByteArray(file));
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
@RequestMapping("download2.do")
public String  xiazai(HttpServletRequest request,HttpServletResponse  response){
  String fileName= request.getRealPath("/upload");
    fileName+="/460.gif"; 
    File file=new File(fileName);
  downLoadFile(response, file);

    return null;
}

}
     <form action="fileupload" method="post">
    <input type="file" name = "file">
    <input type="submit" value="UPLOAD">



    </form>
  • 写回答

1条回答 默认 最新

  • TeriMoni 2017-07-18 05:44
    关注

    form 里面添加 enctype="multipart/form-data" ,你是文件上传,要以二进制传过去。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘