qq_32114925 2017-01-07 08:36 采纳率: 12.5%
浏览 5022

无法运行出现The requested resource is not available.

选择图片点击上传后出现
HTTP Status 404 - /image/UploadPict

type Status report

message /image/UploadPict

description The requested resource (/image/UploadPict) is not available.

Apache Tomcat/6.0.13

package image;
import java.io.File;
import java.io.IOException;

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

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class UploadPict extends HttpServlet{

protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    String id =request.getParameter("imgid");
    System.out.println("ID="+id);

    //上传图片保存路径
    String filePath=getServletContext().getRealPath("/")+"imgs";

    File file=new File(filePath);
    if(!file.exists()){//如果不存在这个路径
        file.mkdirs();//就创建
    }

    SmartUpload su = new SmartUpload();//上传图片的工具类
    su.initialize(getServletConfig(), request, response);// 初始化
    try {
        su.upload();// 上传
        com.jspsmart.upload.File f1 = su.getFiles().getFile(0);//因为只一次只上传一个图片,所以就getFile(0),如果多次还要迭代遍历
         String imageName = f1.getFileName();//得到图片的名字
        int idx = imageName.lastIndexOf(".");
        String imageType = imageName.substring(idx, imageName.length());// 得到图片的类型,比如是 .jpg
        String newImageName = String.valueOf(System.currentTimeMillis());//图片的新名字,最好不要用随机数,因为随机数也可能有一样的
        String path = "E:"+File.separator+"image"+File.separator+File.separator+File.separator+File.separator;
        String imagePath = path+File.separator+newImageName+imageType;//已经保存的图片的绝对路径,下面要对图片重新命名
        //生成保存图片的路径,File.separator是个跨平台的分隔符
        f1.saveAs(imagePath);// 保存图片到这个目录下


    } catch (SmartUploadException e) {
        e.printStackTrace();
    }
}

}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



浏览图片
 请选择上传的图片: <input type="file" name="imgid" id="imgid" value="选择图片">

  • 写回答

3条回答 默认 最新

  • 钟渊 2017-01-08 04:13
    关注

    这样可以获取你页面的file
    protected void service(HttpServletRequest request, HttpServletResponse response,**MultipartFile file**){

    }

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮