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重新部署後,當然就沒有了。