在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文件夹下面。