我有一些代码使用struts2将图像上传到服务器。上传后我想将我的图像显示为缩略图并发送我上传到数据库的图像路径。我混淆了如何使其工作
这是我的Action Code.Cimande Action是从ActionSupport扩展而来的。
package com.maetrika.jagatoko.controller;
import java.io.File;
import java.io.FilePermission;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.blueoxygen.cimande.commons.CimandeAction;
public class FileUpload extends CimandeAction{
private File upload;
private String uploadContentType;
private String uploadFileName;
private String fileCaption;
private HttpServletRequest servletRequest;
File file;
public String execute() throws Exception{
try{
//String filePath=servletRequest.getRealPath("/");
String filePath=servletRequest.getSession().getServletContext().getRealPath("/");
File fileToCreate=new File(filePath, this.uploadFileName);
FileUtils.copyFile(upload, fileToCreate);
}catch (Exception e) {
e.printStackTrace();
return INPUT;
// TODO: handle exception
}
return SUCCESS;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
System.out.println("=======================" + uploadFileName);
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getFileCaption() {
return fileCaption;
}
public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
}
public HttpServletRequest getServletRequest() {
return servletRequest;
}
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
我使用力度进行观察。 这是我的上传图片代码
<html>
<head>
</head>
<body>
<form method="post" action="doUpload" enctype="multipart/form-data">
<table border="0" cellpadding="2">
<tr>
<td>Upload Gambar Produk</td>
</tr>
<tr>
<td><input type="file" name="upload"></td>
</tr>
<tr>
<td><input type="submit" value="submit"/>
</tr>
</table>
</form>
</body>
</html>
我是java编程的新手。请帮帮我..
答案 0 :(得分:2)
您必须仅在数据库中存储图像名称,以毫秒为单位生成时间戳以避免重复
String fileName = myFile.getFileName();
String extension = "";
if (fileName != null && !fileName.equals("")) {
extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
}
long longName = Calendar.getInstance().getTimeInMillis(); //date in miliseconds
String newFileName = longName + extension;
现在,您希望将图像存储在项目中以备将来使用。为此,在您的根文件夹中创建一个名为userimages的文件夹,您的Action代码将如下所示:
String uploadFolder = "userimages";
String filePath = getServlet().getServletContext().getRealPath("/") + uploadFolder;
/* Save file on the server */
if (!fileName.equals("")) {
System.out.println("Server path:" + filePath);
//Create file
File fileToCreate = new File(filePath, newFileName);
//If file does not exists, create file
if (!fileToCreate.exists()) {
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
fileOutStream.write(myFile.getFileData());
fileOutStream.flush();
fileOutStream.close();
}
}
现在,要在你的jsp中显示图像,你需要使用EL
类似的东西:
<img src="${pageContext.request.contextPath}/userimages/${imagename}"/>
我希望能帮到你