作为一个例子,我已经转换了一个带有重新调整大小的图像的canvas元素,并将其发布到一个隐藏的输入字段中,该字段现在编码为
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Flags.Flag;
import javax.mail.search.FlagTerm;
public class ReadMailSample {
Properties properties = null;
private Session session = null;
private Store store = null;
private Folder inbox = null;
private String userName;
private String password;
String downloadDirectory = "D:/Attachment/";
public ReadMailSample() {
userName = "myemail@domain.com";// provide user name
password = "mypassword";// provide password
}
public void readMails() {
properties = new Properties();
properties.setProperty("mail.host", "imap.secureserver.net");
properties.setProperty("mail.port", "110");
properties.setProperty("mail.transport.protocol", "imap");
session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
try {
store = session.getStore("imap");
store.connect();
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.search(new FlagTerm(
new Flags(Flag.SEEN), false));
;
System.out.println("Number of mails = " + messages.length);
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
Address[] from = message.getFrom();
System.out.println("--> ** E-mail No. "+ i + " **");
System.out.println("-------------------------------");
System.out.println("Date : " + message.getSentDate());
System.out.println("From : " + from[0]);
System.out.println("Subject: " + message.getSubject());
System.out.println("Content :");
processMessageBody(message);
System.out.println("--------------------------------");
}
inbox.close(true);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void processMessageBody(Message message) {
try {
Object content = message.getContent();
// check for string
// then check for multipart
if (content instanceof String) {
System.out.println(content);
} else if (content instanceof Multipart) {
System.out.println(content);
Multipart multiPart = (Multipart) content;
procesMultiPart(multiPart);
} else if (content instanceof InputStream) {
InputStream inStream = (InputStream) content;
int ch;
while ((ch = inStream.read()) != -1) {
System.out.write(ch);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void procesMultiPart(Multipart content) {
try {
for (int i = 0; i < content.getCount(); i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o;
o = bodyPart.getContent();
if (o instanceof String) {
System.out.println("Text = " + o);
} else if (null != bodyPart.getDisposition()
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)) {
String fileName = bodyPart.getFileName();
System.out.println("Text = " + o);
System.out.println("fileName = " + fileName);
InputStream inStream = bodyPart.getInputStream();
FileOutputStream outStream = new FileOutputStream(new File(
downloadDirectory + fileName));
byte[] tempBuffer = new byte[4096];// 4 KB
int numRead;
while ((numRead = inStream.read(tempBuffer)) != -1) {
outStream.write(tempBuffer);
}
inStream.close();
outStream.close();
}
// else?
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ReadMailSample sample = new ReadMailSample();
sample.readMails();
}
}
然后将此值发布到我需要将此字符串转换为图像并保存到服务器上的同一页面。
文件后面的代码(upload.aspx)
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...
我很确定在保存到服务器之前我需要将imagedata转换为二进制文件,但我无法理解我需要修改上面的代码。有任何想法吗?保存到服务器的代码不起作用。
一旦我将其转换为图像并将其更改为上面的名称 - 我将其通过LINQ存储回数据库 - 并附加一个URL。
非常感谢任何帮助。
答案 0 :(得分:3)
希望以下功能有所帮助。
public string ImageToBase64(Image image,
System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
编辑1 -
从评论看来,你似乎得到了base64字符串,你需要将其保存为服务器上的图像,然后在需要时你需要使用物理服务器路径显示该图像。
确定。 Base64ToImage将为您的base64字符串提供图像。您可以使用
将其保存在服务器上image.Save("PATH", System.Drawing.Imaging.ImageFormat.Jpeg);
这个&#34; PATH&#34;您提供或创建的可以作为URL存储在DB中,您可以在显示时使用它。
注意:确保您对要保存图像的文件夹具有写入权限。
修改-2 强> 您的功能应如下所示。请根据需要输入验证码,错误处理。
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile filePosted = Request.Files["newinput"];
string base64String = filePosted.ToString();
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile);
image.Save(filepath,ImageFormat.Jpeg);
}