请求帮助 我把图片上传到服务器端,怎么在获得这个图片啊
我获得图片在tomcat下的路径 为什么获得不到图片??
我获得图片在tomcat下的路径 为什么获得不到图片??
解决方案 »
- javascript调用java方法
- 急急急急~~~~ 报错!!org.apache.jasper.JasperException java.lang.NullPointerException
- WebNoteEditor V2 Beta 在线编辑器 For Jsp版本
- 在发布项目时遇到的问题?
- 解决中文乱码 送高分
- 今天答辩,导师问到一个这样的问题~
- 在web中如何使用active
- 使用UltraEdit 时怎么让 jsp文件也高亮显示?
- javascript问题,白送分
- JSP中如何将一段从数据库中取出来的文字(一个字段),最后让它在文本框中分段显示出来
- 菜鸟提问:对于JDBC模糊查询时候.你们是怎么防止SQL注入的.
- javascript中的test方法
<td align="right">业务图片:</td>
<td><input type="file" name="file" id="imgurl" style="border:1px solid #7F9DB9;"/><span class="checkts"> *</span><span class="sm"> 说明:游戏图片</span><br/></td>
</tr>
js代码:var imgurlValue = $('#imgurl').val(); // 图片上传
if ((imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "jpg")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "jpeg")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "png")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "bmp")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "gif")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "JPG")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "JPEG")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "PNG")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "BMP")
&&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "GIF")) {
//alert("系统不允许上传此种类型文件!");
$('#errorMsg').html("系统不允许上传此种类型文件");
return false;
}
action 代码:// ********************************************************
//首先将文件写入一个输入流里面
InputStream is = new FileInputStream(file);
//其次得到你要上传文件到那个目录
String root = ServletActionContext.getRequest().getRealPath("/upload/images");
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileFileName.indexOf(".");
String extension = fileFileName.substring(position) ;
String newFileName = formatDate + random + extension ;
//再次创建一个File来保存你的文件
File destFile = new File(root,newFileName);
//然后就是一个输出流将文件写入到File中。
OutputStream os = new FileOutputStream(destFile);
//以下就是写入文件的方式
byte buffer[] = new byte[2048];
int length = 0;
while((length = is.read(buffer))>0){
os.write(buffer,0,length);
}
//最后一定要关闭流
is.close();
os.close();
private File imageFile;getter/setter....FileInputStream in = new FileInputStream(imageFile);//commons-fileupload-1.2.1.jar
这是操作系统路径 用这个路径得不到这个图片
把图片的名称保存到数据库然后读取出来加上路径的名称就ok
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'upload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<center>
<form method="post" action="${pageContext.request.contextPath}/uploadAction.action" enctype="multipart/form-data">
File:<input type="file" name="pic"></input><br />
<input type="submit" value="上传图片"></input>
</form>
</center>
</body>
</html>servlet代码package action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport
implements ServletRequestAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private File pic;
private String picFileName;
private HttpServletRequest request;
public String getPicFileName() {
return picFileName;
} public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
} public File getPic() {
return pic;
} public void setPic(File pic) {
this.pic = pic;
} @Override
public String execute() throws Exception {
ServletContext sc = request.getSession().getServletContext();
String absoultPath = sc.getRealPath("/file");
FileInputStream in = new FileInputStream(pic);
FileOutputStream out = new FileOutputStream(absoultPath+"/"+picFileName);
byte[] b = new byte[1024];
int length = 0;
while(true){
length = in.read(b);
if(length<0)break;
out.write(b,0,length);
}
in.close();
out.close();
System.out.println(picFileName+"bbbbbbbbbbbbbbbb");
request.getSession().setAttribute("pi",pic);
System.out.println("file is "+ pic);
return "ok";
} public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}成功跳转页面}<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'uploadOK.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<center><h1>
<img src="${pageContext.request.contextPath}/${sessionScope.pic}"/>
</h1></center>
</body>
</html>已经成功跳转到uploadOK,可以使这个页面仅输出一句话,怎么显示图片,很纠结,谢谢各位了
forum_system\upload__7ac4141d_131f5f9ce44__8000_00000000.tmp
楼主你见过这种格式的图片吗?
TempFilePath = C:\\1\\
RealFilePath = C:\\2\\String filepath = PropertyUtil.getProperty("TempFilePath");
String drcPath = filepath+compid+File.separatorChar;
File drcpath = new File(drcPath);
File file =new File(drcPath+ this.getUploadFileName());
if(!drcpath.exists()){
drcpath.mkdirs(); }
if(!file.exists()){
02Service.copy(this.upload, file);
// file.delete();
}
return SUCCESS;
public void copy(File src, File dst){
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), Ass02Constant.BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), Ass02Constant.BUFFER_SIZE);
byte [] buffer = new byte [Ass02Constant.BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
通过窗口信息提示看到文件创建后又被删除了 所以你需要将你的临时文件copy到你要上传的目标下面这样就可以找到了 希望能帮助楼主