superjcn 2012-09-07 15:05
浏览 628
已采纳

关于上传图片到服务器的路径问题!请大家帮一下忙

在jsp页面上显示服务器的图片:

<img src="http://www.xxxx.com:serverPort/image/gfan.png"  id="myImg"  title="证件照(反面)" />

 这样显示是没有问题的。

我在jsp页面上传图片到该服务器:

<form name="myform" action="upload!uploadfile" method="post"  enctype="multipart/form-data">
<input type="file" id="filePath" name="filePath"/>
<input type="submit"  id="mysubmit" value="上传"/>
</form>

  action:

package com.zhangying.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class FileUpload extends ActionSupport implements ServletRequestAware,ServletResponseAware{

    /**
     * author : Jacen
     * FileUpload Action
     * upload files from localhost to server
     */
    private static final long serialVersionUID = 1L;
    private String filePath;
    private String serverPath;
    private HttpServletResponse response;
    private HttpServletRequest request;
    
    public String uploadfile()
            throws Exception {
        
        System.out.println(filePath);    //参数1:本地文件路径
//      System.out.println(serverPath);  //参数2:要上传的服务器路径
        
        String resultPath = ""; // 上传后图片所在的路径
        FileOutputStream out = null; // 文件输出流
        
        try {     // 验证图片上传的格式是否正确
            File f = new File(filePath);
            if (!f.isFile()) {
                throw new Exception(" f不是图片文件!");
            }
            if (f != null && f.exists()) { // 这里的ImageIO属于java工厂类,在工厂类class里面,调用的System.gc(),频繁调用会造成dump,需要考虑优化
                BufferedImage image = ImageIO.read(f); // 读入文件
                if (image != null) {
                    BufferedImage tag = new BufferedImage(image.getWidth(),
                            image.getHeight(), BufferedImage.TYPE_INT_RGB); // 构造一个类型为预定义图像类型之一的 BufferedImage
                    tag.getGraphics().drawImage(image, 0, 0, image.getWidth(),
                            image.getHeight(), null); // 绘制所需要尺寸大小的图片
                    /*
                     * 以下生成 图片上传后在服务器上的 新路径
                     */
                    int lastLength = filePath.lastIndexOf(".");
                    String imageName="jacen";  //用用户名来作为上传图片的文件名
                    String fileType = filePath.substring(lastLength); // 获取上传图片的类型
                    
                    resultPath = "http://www.gyswad.com:90/developerCerpic/"+ imageName+ fileType;  //图片在服务器上的地址
                    System.out.println(resultPath);
                    /*
                     * 进行图片的绘制
                     */
                    out = new FileOutputStream(resultPath);
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    JPEGEncodeParam param = encoder
                            .getDefaultJPEGEncodeParam(tag);
                    param.setQuality(0.95f, true); // 95%图像
                    param.setDensityUnit(1); // 像素尺寸单位.像素/英寸
                    param.setXDensity(300); // 水平分辨率
                    param.setYDensity(300); // 垂直分辨率
                    encoder.setJPEGEncodeParam(param);
                    encoder.encode(tag);
                    tag = null;
                }
            }
            f = null;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            out.close();
            out=null;
        }
//      request.setAttribute("resultPath", resultPath);
//      return resultPath;
        return "success";
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getServerPath() {
        return serverPath;
    }

    public void setServerPath(String serverPath) {
        this.serverPath = serverPath;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setServletRequest(HttpServletRequest arg0) {
        // TODO Auto-generated method stub
        
    }
    
}

  现在报错:说是空指向异常

finally{
            out.close();
            out=null;
        }

我感觉是resultPath<!--上传图片到服务器的路径-->有问题,请各位帮忙看一下,这里的resultPath到底怎么写?还有,如果各位还有什么更好的方法实现图片上传,请指教。本人新手,一切都略知。

控制台打印的错误信息是这样的:

\temp\upload__daca63c_1399f88364d__8000_00000000.tmp
http://www.gyswad.com:90/developerCerpic/jacen.tmp
java.io.FileNotFoundException: http:\www.gyswad.com:90\developerCerpic\jacen.tmp (文件名、目录名或卷标语法不正确。)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at com.zhangying.util.FileUpload.uploadfile(FileUpload.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

 补充:我想将本地图片上传到服务器的d://Apache2.2/temp/image文件夹下面。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 写回答

5条回答 默认 最新

  • jinnianshilongnian 2012-09-07 15:14
    关注

    1、private String filePath; 修改为private File filePath 可以直接得到上传的文件;(注意文件名是一个临时文件名 以tmp结尾)
    2、private String filePathFileName 可以得到上传的文件名
    3、上传文件后默认放到 javax.servlet.context.tempdir 指定的临时目录中, 可以在struts2配置文件添加struts.multipart.saveDir 来指定临时目录

    4、软连接 如web应用/upload -----> /Apache/temp/image
    5、rootPath = getServletContext().getRealPath("/upload") 得到upload目录
    6、resultPath =rootPath + "/" + filePathFileName;

    7、注意上传文件名有重名情况,比如在filePathFileName前边加上时间戳等解决

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波