1、在Struts2中如何上传图片并将刚刚上传的图片立即显示出来
//action的关键代码
import com.xt.gyz.facelook.service.FacesService;
public class ShowPic extends ActionSupport {
private InputStream is;
private Integer id;
private FacesService facesService;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setFacesService(FacesService facesService) {
this.facesService = facesService;
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
@Override
public String execute() throws Exception {
Faces face=facesService.findById(id);
is=new ByteArrayInputStream(face.getPic());
// FileOutputStream fos=new FileOutputStream(new File("E:/body.jpg"));
// System.out.println(fos);
// System.out.println("face:"+face.getPic().length);
// fos.write(face.getPic());
// fos.flush();
// fos.close();
return super.execute();
}
}
//不要忘记struts.xml配置
......
<action name="showPic" class="showPicAction">
<result type="stream">
<param name="inputName">is</param>
<param name="contectType">image/jpg</param>
</result>
</action>
.....
这样就可以啦
2、struts2上传图片怎么保存到数据库中
数据库保存的是该文件的一些信息,文件保存在服务器指定的目录里。
Struts2文件上传保存到数据库中,可以用Fileupload组件。
----------------------struts2文件上传实例如下----------------
package com.ssh.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String username;
private String password;
private File file;
private String fileFileName;
private String fileContectType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContectType() {
return fileContectType;
}
public void setFileContectType(String fileContectType) {
this.fileContectType = fileContectType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception {
InputStream is = new FileInputStream(file);
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName());
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while((length=is.read(buffer)) > 0)
{
os.write(buffer);
}
is.close();
os.close();
return SUCCESS;
}
}
3、struts2+Hibernate上传图片,如何实现图片可以放在服务器上;数据库保存图片路径。
楼主struts2上传代码会写吗?
struts2本身提供了上传拦截器,用struts2实现上专传功能,并获得保存地址
<!属-- 配置fileUpload的拦截器 -->
<interceptor-ref name="fileUpload">
获得文件存储地址(上传时指定)+文件名
调用保存方法(Hibernate),将文件路径存入数据库
在一个事务中完成即可!
如:有哪块有难点,及时追问。good luck!
4、使用struts2 怎么实现 文件上传到另外一台电脑
传输文件的功能,类似于网络聊天程序。
肯定要用的文件传输用的回IO流,还有网络答通信用到的socket了。
可以在你部署struts2网站的服务器上面写一个网络通信程序(服务器端),对应的网络通信程序(客户端)放在“另外一台电脑”上面。
网站的服务器启动之后:
1。把那个网络通信程序(服务器端)启动
2。把“另外一台电脑”上面的网络通信程序(客户端)启动,现在两端就建立连接了。
3。可以通过服务器端向客户端发送数据。
以上过程跟我们平时用的聊天程序一样。
你可以在网上看看相应的网络聊天程序,现在网上这样的程序还是很多的。
里面实现了这样的机制。
5、用struts2上传文件将文件上传路径保存在Mysql数据库的问题:
您好,上传到服务器上面去最好了。方便以后下载,
保存路径的话,希望您能在mysql数据库中有个保存路径的字段,专门用来保存url,
假如有需要的话,我这里有个struts2上传的demo。
6、struts2上传文件放到web目录下
当你打抄成WAR包发布的时候
用这个获取的路径ServletActionContext.getServletContext().getRealPath("/upload");
就是在webcontent下了。
所以你这样写就是对的。
7、利用struts2框架上传用户的头像到服务器里,如何在jsp页面中利用<img / >标签获取这个图像呢?
头像地址要用绝对路径保存。
图片要保存在你项目所在服务器容器的应用发布目录下,专要让浏览器可以通过属url地址直接访问到。
将这个url地址保存在数据库中
需要使用的时候把url从数据库中取出来,然后<img / >标签直接指向这个url即可
8、struts2上传图片到服务器读取的时候显示不出来图片是什么问题.图片的路径也是对的,就是显示不来图片
你可以把图片的地址打到浏览器的地址栏中来确认地址是否正确
9、怎样将struts2上传文件保存到数据库中
数据库保存的是该文件的一些信息,文件保存在服务器指定的目录里。
Struts2文件上传保存到数据库中,可以用Fileupload组件。
----------------------struts2文件上传实例如下----------------
package com.ssh.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String username;
private String password;
private File file;
private String fileFileName;
private String fileContectType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContectType() {
return fileContectType;
}
public void setFileContectType(String fileContectType) {
this.fileContectType = fileContectType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception {
InputStream is = new FileInputStream(file);
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName());
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while((length=is.read(buffer)) > 0)
{
os.write(buffer);
}
is.close();
os.close();
return SUCCESS;
}
}
10、Struts2上传文件到服务器下,怎么服务器重启之后文件就没了?
我的也遇到过这样的情况,我的文件是放在服务器上,原因是本地的项目是没有存放该文件的,用myeclipse重新部署后,当然就没有了。