ssh上传图片的问题
就是upload的action及jsp界面form怎么写,图片上传到服务器,然后将名字存入数据库,可以在查询该表示显示图片...
就是upload的action及jsp界面form怎么写,图片上传到服务器,然后将名字存入数据库,可以在查询该表示显示图片
展开
展开全部
上传图片
提交jsp页面:
<s:form action ="sys_fileUpload.action" method ="post" enctype ="multipart/form-data" >
<s:file name ="myFile" label ="Image File" />
<s:textfield name ="caption" label ="Caption" />
<s:submit></s:submit>
</s:form >
action:
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ; //缓冲器大小
private File myFile;
private String caption;
private String myFileContentType;//上传文件的类型
private String myFileFileName;//上传文件的文件名,该文件名不包括文件的路径
private String imageFileName;
public void setMyFilemyFileContentType(String myFileContentType) {
this .myFileContentType = myFileContentType;
}
public void setMyFileFileName(String fileName) {
this .myFileFileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [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();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf( "." );
return fileName.substring(pos);
}
public String execute() {
imageFileName = new Date().getTime() + getExtention(myFileFileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/") +imageFileName);
copy(myFile,imageFile);
return "success";
}
struts配置:
<package name="p0" extends="struts-default">
<action name ="sys_fileUpload" class ="action.FileAction" method="execute" >
<result name ="success" > /success.jsp </result >
</action >
</package>
显示jsp页面:
<img src ='<s:property value ="imageFileName" /> ' />
<br />
<s:property value ="caption" />
以上代码图片地址没有存入数据库,你可以将获取存入地址写入数据库,然后在页面从数据库读出来。
提交jsp页面:
<s:form action ="sys_fileUpload.action" method ="post" enctype ="multipart/form-data" >
<s:file name ="myFile" label ="Image File" />
<s:textfield name ="caption" label ="Caption" />
<s:submit></s:submit>
</s:form >
action:
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ; //缓冲器大小
private File myFile;
private String caption;
private String myFileContentType;//上传文件的类型
private String myFileFileName;//上传文件的文件名,该文件名不包括文件的路径
private String imageFileName;
public void setMyFilemyFileContentType(String myFileContentType) {
this .myFileContentType = myFileContentType;
}
public void setMyFileFileName(String fileName) {
this .myFileFileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [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();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf( "." );
return fileName.substring(pos);
}
public String execute() {
imageFileName = new Date().getTime() + getExtention(myFileFileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/") +imageFileName);
copy(myFile,imageFile);
return "success";
}
struts配置:
<package name="p0" extends="struts-default">
<action name ="sys_fileUpload" class ="action.FileAction" method="execute" >
<result name ="success" > /success.jsp </result >
</action >
</package>
显示jsp页面:
<img src ='<s:property value ="imageFileName" /> ' />
<br />
<s:property value ="caption" />
以上代码图片地址没有存入数据库,你可以将获取存入地址写入数据库,然后在页面从数据库读出来。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询